开发者

Not Applicable for the Arguments

I'm trying to show a progress message when a preference is selected:

        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;
        }
    });

However I'm getting an error in Ecli开发者_C百科pse:

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

However, when I execute the line before the setOnPreferenceClickListener, it compiles fine!

I'm probably revealing my severe inexperience in Java, but would appreate a clue!


That's because in that case you are passing a reference to the inneer activity (an OnPreferenceClickListener) instead of a context (which usually must be your activity). Change it to this and it will work:

ProgressDialog pDialog = ProgressDialog.show(NameOfYourActivity.this, "Location" , "Finding location...", true);


You have to read, really read carefully, the error message the compiler is emitting.

The compiler is complaining about this line:

ProgressDialog pDialog = ProgressDialog.show(this, "Location" , "Finding location...", true);

the ProgressDialog.show() method requires a Context as the first parameter.

You passed this from within the OnPreferenceClickListener class, so you're passing an OnPreferenceClickListener instead of a Context.


this in this context is the OnPreferenceClickListener, not the outer class.

If you want to refer that, you'll have to do

ProgressDialog pDialog = ProgressDialog.show(YourClassName.this, "Location" , "Finding location...", true);

YourClassName being the class of your preference activity (or whatever you're in).


I am using this on my Async Task.this is works like charm.

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            dialog = new ProgressDialog(ActivityAddTicket.this);
            dialog.setTitle(R.string.processing);
            dialog.setMessage(getResources().getString(R.string.loading));
            dialog.show();
        };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜