Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.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 如何从主活动对话框&x27;调用FindReference;s onClick函数?_Android_Android Preferences_Android Dialog - Fatal编程技术网

Android 如何从主活动对话框&x27;调用FindReference;s onClick函数?

Android 如何从主活动对话框&x27;调用FindReference;s onClick函数?,android,android-preferences,android-dialog,Android,Android Preferences,Android Dialog,我在主活动中有以下对话框: @Override protected Dialog onCreateDialog(int id) { final Dialog dialog; switch(id) { case DIALOG_NAME: builderDialog.setMessage(getString(R.string.dialog_text)) .setCancelable(false) .s

我在主活动中有以下对话框:

@Override
protected Dialog onCreateDialog(int id) {
    final Dialog dialog;
    switch(id) {
    case DIALOG_NAME:
        builderDialog.setMessage(getString(R.string.dialog_text))
               .setCancelable(false)
               .setPositiveButton(R.string.dialog_share_yes, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // how should I call findPreference here?
                       PreferenceScreen screen = (PreferenceScreen) findPreference("pref_key");
                       ...
                   }
               })
我得到以下错误:

对于新类型,未定义FindReference(String)方法 DialogInterface.OnClickListener(){}


你必须使用你的上下文来调用它

YourActivity.this.findPreference("pref_key");

这应该行得通;)

我应该使用什么作为您的活动?我的主要活动是什么?然后它就不起作用了:类型MyActivity的方法findReference(String)是未定义的。MyActivity扩展ListActivity:public类MyActivity扩展ListActivity{。然后在activity:Context mContext;和onCreate中,do:mContext=getBaseContext();然后在对话框中使用mContext.findReference(“pref_键”)@SebastianBreit——我已经声明了
Context-mContext
,在
onCreate
中我有
mContext=getBaseContext();
但是我没有
对话框
。如果我把
mContext.findPreference(“key”);
放在
onCreate中,我会得到“无法解析findPreference(String)