Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:带有AlertDialog的首选项_Android_Android Fragments - Fatal编程技术网

Android:带有AlertDialog的首选项

Android:带有AlertDialog的首选项,android,android-fragments,Android,Android Fragments,我的on首选项单击Listener创建一个新的AlertDialog,如下所示。当我运行程序时,我得到了期望值 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 我的代码有什么问题 public static class PrefsFragment extends PreferenceFragment { Preferenc

我的
on首选项单击Listener
创建一个新的
AlertDialog
,如下所示。当我运行程序时,我得到了期望值

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
我的代码有什么问题

public static class PrefsFragment extends PreferenceFragment {
    Preference pref= findPreference("text_preference1");
    pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            new AlertDialog.Builder(mContext).setTitle(R.string.alert_dialog_title)
                    .setMessage(R.string.alert_dialog_message)
                    .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    })
                    .setNegativeButton(R.string.alert_dialog_nein, null).show();

                    //do s.th.
        return false;
        }
    });
}

我想你得到的是
ApplicationContext
。但它不应该用于创建对话框

您不应该使用
new AlertDialog.Builder(mContext)
中的
mContext
,它返回与片段关联的活动

因为您使用的是片段,所以只需调用片段
getActivity()
方法即可获取活动的上下文