rstemme rstemme - 5 months ago 138
Android Question

Start Activity and Pass Input through DialogFragment

I am using a dialog to receive a user input for a filename. A button in the main activity launches the dialog, at which point I would like the positive button to store the input and then start the next activity, passing the input to the next activity. Whenever I press the positive button in the dialog, the app crashes. I have tried storing the input inside and outside of the onClick() within the dialog as well as trying to start the activity separately outside of the dialog. I am new to Android, so much of the code is sampled from the API guides. Why does the app keep crashing and how do I start the next activity?

public class initializeTestFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.activity_initialize_test, null));
builder.setTitle(R.string.start_test);
// Add action buttons
builder.setPositiveButton(R.string.start_test, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText editText = (EditText) findViewById(R.id.filename);
String filename = editText.getText().toString();
Context context = getContext();
Intent intent = new Intent(context, InitializeTest.class);
intent.putExtra(FILENAME, filename);
context.startActivity(intent);
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
return builder.create();
}
}

public void startTest(View view) {
DialogFragment newFragment = new initializeTestFragment();
android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
newFragment.show(fragmentTransaction, "initialize");
}


EDIT: As requested, here is the full logcat from running the app (I have a OnePlus One on Android 6.0.1, Cyanogen OS 13.0-ZNH0EAS2JK)

06-07 13:05:23.705 11193-11193/com.tiem.rstemme.layoutpractice I/art: Late-enabling -Xcheck:jni
06-07 13:05:23.764 11193-11193/com.tiem.rstemme.layoutpractice W/System: ClassLoader referenced unknown path: /data/app/com.tiem.rstemme.layoutpractice-2/lib/arm
06-07 13:05:26.165 11193-11193/com.tiem.rstemme.layoutpractice W/System: ClassLoader referenced unknown path: /data/app/com.tiem.rstemme.layoutpractice-2/lib/arm
06-07 13:05:26.181 11193-11193/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package common
06-07 13:05:26.183 11193-11193/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package com.cyngn.hexo
06-07 13:05:26.186 11193-11193/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package com.cyngn.hexo
06-07 13:05:26.223 11193-11193/com.tiem.rstemme.layoutpractice I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
06-07 13:05:26.410 11193-11203/com.tiem.rstemme.layoutpractice W/art: Suspending all threads took: 6.204ms
06-07 13:05:26.539 11193-11193/com.tiem.rstemme.layoutpractice W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-07 13:05:26.631 11193-11193/com.tiem.rstemme.layoutpractice W/Activity: Can reqeust only one set of permissions at a time
06-07 13:05:26.658 11193-11484/com.tiem.rstemme.layoutpractice D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-07 13:05:26.721 11193-11484/com.tiem.rstemme.layoutpractice I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (Ia6c73e7530)
OpenGL ES Shader Compiler Version: E031.29.00.00
Build Date: 12/04/15 Fri
Local Branch: mybranch17080070
Remote Branch: quic/LA.BF.1.1.3_rb1.5
Local Patches: NONE
Reconstruct Branch: NOTHING
06-07 13:05:26.725 11193-11484/com.tiem.rstemme.layoutpractice I/OpenGLRenderer: Initialized EGL, version 1.4
06-07 13:05:31.422 11193-11193/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-07 13:05:31.422 11193-11193/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-07 13:05:32.726 11193-11193/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-07 13:05:32.726 11193-11193/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-07 13:06:09.663 12461-12461/com.tiem.rstemme.layoutpractice W/System: ClassLoader referenced unknown path: /data/app/com.tiem.rstemme.layoutpractice-2/lib/arm
06-07 13:06:10.314 12461-12461/com.tiem.rstemme.layoutpractice W/System: ClassLoader referenced unknown path: /data/app/com.tiem.rstemme.layoutpractice-2/lib/arm
06-07 13:06:10.319 12461-12461/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package common
06-07 13:06:10.321 12461-12461/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package com.cyngn.hexo
06-07 13:06:10.323 12461-12461/com.tiem.rstemme.layoutpractice I/LoadedApk: No resource references to update in package com.cyngn.hexo
06-07 13:06:10.353 12461-12461/com.tiem.rstemme.layoutpractice I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
06-07 13:06:10.602 12461-12461/com.tiem.rstemme.layoutpractice W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-07 13:06:10.687 12461-12461/com.tiem.rstemme.layoutpractice W/Activity: Can reqeust only one set of permissions at a time
06-07 13:06:10.710 12461-12512/com.tiem.rstemme.layoutpractice D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-07 13:06:10.774 12461-12512/com.tiem.rstemme.layoutpractice I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (Ia6c73e7530)
OpenGL ES Shader Compiler Version: E031.29.00.00
Build Date: 12/04/15 Fri
Local Branch: mybranch17080070
Remote Branch: quic/LA.BF.1.1.3_rb1.5
Local Patches: NONE
Reconstruct Branch: NOTHING
06-07 13:06:10.775 12461-12512/com.tiem.rstemme.layoutpractice I/OpenGLRenderer: Initialized EGL, version 1.4
06-07 13:06:12.644 12461-12461/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-07 13:06:12.644 12461-12461/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-07 13:06:13.308 12461-12461/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-07 13:06:13.308 12461-12461/com.tiem.rstemme.layoutpractice E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-07 13:06:15.655 12461-12461/com.tiem.rstemme.layoutpractice D/AndroidRuntime: Shutting down VM


--------- beginning of crash
06-07 13:06:15.655 12461-12461/com.tiem.rstemme.layoutpractice E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tiem.rstemme.layoutpractice, PID: 12461
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.tiem.rstemme.layoutpractice.MainActivity$initializeTestFragment$1.onClick(MainActivity.java:93)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-07 13:06:18.119 12461-12461/com.tiem.rstemme.layoutpractice I/Process: Sending signal. PID: 12461 SIG: 9

Answer

I'm assuming that the filename EditText is in your inflated layout. You have to reference it via that inflated layout.

View view = inflater.inflate(R.layout.activity_initialize_test, null);
final EditText editText = (EditText) view.findViewById(R.id.filename);

builder.setPositiveButton(R.string.start_test, new DialogInterface.OnClickListener() {            
    @Override
    public void onClick(DialogInterface dialog, int id) {
        String filename = editText.getText().toString();
        Context context = getContext();
        Intent intent = new Intent(context, InitializeTest.class);
        intent.putExtra(FILENAME, filename);
        context.startActivity(intent);
    }
});

You are probably getting a NullPointerException due to findViewById searching through your activity layout instead of your dialog layout and not finding a corresponding view with that id.

Comments