0

I have the following dialog in main activity:

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

I am getting the following error:

The method findPreference(String) is undefined for the type new DialogInterface.OnClickListener(){}

LA_
  • 19,823
  • 58
  • 172
  • 308

1 Answers1

2

You must call this using your context;

YourActivity.this.findPreference("pref_key");

that should work ;)

Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
  • What should I use as `YourActivity`? Is is name of my main activity? Then it doesn't work: _The method findPreference(String) is undefined for the type MyActivity_. MyActivity extends ListActivity: _public class MyActivity extends ListActivity {_. – LA_ May 01 '12 at 14:41
  • then create a local variable in your activity: Context mContext; and in the onCreate, do: mContext=getBaseContext(); then in your dialog use mContext.findPreference("pref_key"); – Sebastian Breit May 02 '12 at 23:26
  • @SebastianBreit--I have declared `Context mContext` and in `onCreate` I have `mContext = getBaseContext();` but I don't have a `Dialog`. If I put `mContext.findPreference("key");` in `onCreate, I get 'Cannot resolve findPreference(String)` – DSlomer64 Jun 04 '18 at 21:45