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)