通过DialogFragment启动活动并传递输入

时间:2016-06-07 15:58:52

标签: java android alertdialog

我正在使用对话框来接收文件名的用户输入。主活动中的一个按钮启动对话框,此时我希望正面按钮存储输入,然后启动下一个活动,将输入传递给下一个活动。每当我按下对话框中的肯定按钮时,应用程序就会崩溃。我已经尝试在对话框中存储onClick()内部和外部的输入,并尝试在对话框外单独启动活动。我是Android新手,因此大部分代码都是从API指南中提取的。为什么应用程序会继续崩溃?如何开始下一个活动?

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");
}

编辑:根据要求,这是运行应用程序的完整logcat(我在Android 6.0.1上安装了OnePlus One,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

1 个答案:

答案 0 :(得分:2)

我假设filename EditText在你的虚增布局中。你必须通过那个膨胀的布局来引用它。

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);
    }
});

由于NullPointerException搜索您的活动布局而非您的对话框布局而没有找到具有该ID的相应视图,您可能会收到findViewById