1

I have the activity (in which the function is called), the java file holding all the functions and an activity that uses the camera to scan codes.

When the main activity loads it calls the generateTextView function which populates the page with TextViews. The onClick is enabled on all of them. When one TextView is clicked it launches the camera activity to scan a barcode. How can I send that barcode string back to the main activity, to the TextView the camera activity was launched on? I keep encountering a null object reference.

I need to tab each one of those dynamically generated TextViews and send the barcode data back to it on the main activity.

LayoutFile

public class LayoutElements extends AppCompatActivity {

    TextView tv;

    public TextView textViewGenerate(final Context ct, String tag, Integer id) {

        tv = new TextView(ct);

        GradientDrawable gd = new GradientDrawable();
        gd.setColor(0xFFFFFF);
        gd.setCornerRadius(4);
        gd.setStroke(1, 0xFF757575);

        tv.setBackground(gd);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                1.0f
        );

        //lp.height = 50;

        tv.setPadding(7, 9, 0, 0);
        tv.setGravity(Gravity.START);
        tv.setTextSize(22);

        tv.setTag(tag);
        tv.setId(id);
        tv.setHint("Enter Module Serial Number");

        //Click to launch camera
        tv.setClickable(true);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(ct, MctCameraActivity.class);
                Intent intent = new Intent(ct, MctCameraActivity.class);
                startActivityForResult(intent, 1);
                //ct.startActivity(intent);
                //ToDo: finish receiving the data from the activityForResult
            }
        });

        lp.setMargins(10, 0, 10, 0);
        lp.setMarginStart(10);
        lp.setMarginEnd(10);

        tv.setLayoutParams(lp);

        return tv;
    }

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

        if(requestCode == 1 && resultCode == RESULT_OK && data != null) {
            String returnResult = data.getStringExtra("result");
            tv.setText(returnResult);
        }
    }
}

Error Log

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.rsscanner, PID: 16095 java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference at android.app.Activity.startActivityForResult(Activity.java:4225) at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676) at android.app.Activity.startActivityForResult(Activity.java:4183) at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663) at com.example.rsscanner.models.LayoutElements$1.onClick(LayoutElements.java:62) at android.view.View.performClick(View.java:5610) at android.view.View$PerformClick.run(View.java:22265) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

  • Does this answer your question? [java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread](https://stackoverflow.com/questions/53748248/java-lang-nullpointerexception-attempt-to-invoke-virtual-method-android-app-ac) – Nicolas May 21 '20 at 18:24
  • Unfortunately it does not. I pass in the context from the main activity when calling this class and use it as a parameter when dynamically setting the textviews. – TrickExplanation May 21 '20 at 18:36

1 Answers1

0

Your class LayoutElements extends AppCompatActivity, but it looks like you aren't using it like an Activity. You cannot create an instance of an Activity using new. Only Android can instantiate Android components (Service, Activity, BroadcastReceiver, Provider). Your class LayoutElements isn't an Activity and should not extend AppCompatActivity. If you do that, you will see that startActivityForResult() is not defined. This is correct.

To call that method, you need to call it from the Activity. You could change this:

 Intent intent = new Intent(ct, MctCameraActivity.class);
 startActivityForResult(intent, 1);

to this:

Intent intent = new Intent(activity, MctCameraActivity.class);
activity.startActivityForResult(intent, 1);

and change the method signature from:

public TextView textViewGenerate(final Context ct, String tag, Integer id) {

to:

public TextView textViewGenerate(final Activity activity, String tag, Integer id) {

Make sure that when you call textViewGenerate() you pass the current Activity as the first parameter.

NOTE: You need to move onActivityResult() to the actual Activity class that calls textViewGenerate(), as it will be called on the Activity that calls startActivityForResult().

David Wasser
  • 93,459
  • 16
  • 209
  • 274