Android Camera App

DylanB16

New member
Jan 17, 2025
4
0
1
Good morning everyone,

First post, not an Android Developer by any means but I have taken a class on it.

I am looking to create a simple app that allows user to enter in a truck load number, once the number is entered it will hold that number in a variable and concatenate it with a time stamp.

I was able to get that tested and working.

I have a button that checks that a load number was entered before attempting to open the camera and create a file using that name.

I am getting the following errors when attempting to run:

FATAL EXCEPTION: main
Process: com.example.truckloadtest, PID: 18083
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.truckloadtest/com.example.truckloadtest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2323)
at com.example.truckloadtest.MainActivity.<init>(MainActivity.java:30)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)




I will respond to this thread with the code since I have reached the character limit


Dylan
 

DylanB16

New member
Jan 17, 2025
4
0
1
MainActivity.java

Java:
package com.example.truckloadtest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;

public class MainActivity extends Activity
{
    ///////////////////////////////////
    //          Variables
    ///////////////////////////////////

    final String[] holdLoadNumber = new String[1];
    final EditText truckLoadNumber = findViewById(R.id.truckLoadInput);
    final Button   buttonPhoto = findViewById(R.id.photoButton);
    final int      CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    final String   tag = "";
    Uri            photoUri;



    ///////////////////////////////////
    //          On Create
    ///////////////////////////////////

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        ///////////////////////////////////
        //     Button to Take Photo
        ///////////////////////////////////

        buttonPhoto.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (!truckLoadNumber.getText().toString().isEmpty())
                {
                    SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
                    String format      = s.format(new Date());
                    holdLoadNumber[0]  = truckLoadNumber.getText().toString() + '_' + format;

                    Log.d(tag, Arrays.toString(holdLoadNumber));

                    openCamera();
                }

                else
                {
                    Toast.makeText(MainActivity.this, "Please enter load number",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
    }


    ///////////////////////////////////
    //      Open Camera Method
    ///////////////////////////////////
    public void openCamera()
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (intent.resolveActivity(getPackageManager()) != null)
        {
            File photoFile = createImageFile();

            if (photoFile != null)
            {
                photoUri = Uri.fromFile(photoFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }

            else
            {
                Toast.makeText(MainActivity.this, "Shits fucked man",
                        Toast.LENGTH_LONG).show();
            }
        }
    }

    ///////////////////////////////////
    //      Create File Method
    ///////////////////////////////////

    public File createImageFile()
    {
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

        try
        {
            File imageFile = new File(storageDir, Arrays.toString(holdLoadNumber) + ".png");
            imageFile.createNewFile();
            return imageFile;
        }
        catch (IOException e)
        {
            Log.d(tag, Objects.requireNonNull(e.getMessage()));
            return null;
        }
    }


    ///////////////////////////////////
    //   On Activity Result Method
    ///////////////////////////////////

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
        {
            if (resultCode == RESULT_OK)
            {
                Toast.makeText(MainActivity.this, "I think it worked",
                        Toast.LENGTH_LONG).show();

                finish();
            }
            else
            {
                Toast.makeText(MainActivity.this, "Didn't fucking work",
                        Toast.LENGTH_LONG).show();
            }
        }
    }
}
 

DylanB16

New member
Jan 17, 2025
4
0
1
AndroidManifest.xml

XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-feature android:name="android.hardware.camera" android:required="true"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TruckloadTest"
        tools:targetApi="31">
        <activity
            android:name="MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
 

B. Diddy

Senior Ambassador
Moderator
Mar 9, 2012
167,668
7,913
113
Welcome to Android Central! I moved this to the Software Development forum for more specific traffic.