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)