不适用于参数

Hei*_*sis 2 java android

我正在尝试在选择首选项时显示进度消息:

        Preference prefLocation = (Preference) findPreference("location");
    prefLocation.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            ProgressDialog pDialog = ProgressDialog.show(this, "Location" , "Finding location...", true);
            return true;
        }
    });
Run Code Online (Sandbox Code Playgroud)

但是我在Eclipse中遇到错误:

The method show(Context, CharSequence, CharSequence, boolean) in the type ProgressDialog is not applicable for the arguments (new Preference.OnPreferenceClickListener(){}, String, String, boolean)
Run Code Online (Sandbox Code Playgroud)

但是,当我在setOnPreferenceClickListener之前执行该行时,它编译得很好!

我可能已经揭示了我在Java方面的严重缺乏经验,但是会找到一条线索!

Cri*_*ian 6

那是因为在这种情况下,您传递的是对inneer活动的引用(an OnPreferenceClickListener)而不是上下文(通常必须是您的活动).将其更改为此,它将起作用:

ProgressDialog pDialog = ProgressDialog.show(NameOfYourActivity.this, "Location" , "Finding location...", true);
Run Code Online (Sandbox Code Playgroud)