Hein du Plessis
Hein du Plessis

Reputation: 3357

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 Eclipse:

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!

Upvotes: 2

Views: 3797

Answers (4)

user1414160
user1414160

Reputation:

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

Upvotes: 0

EboMike
EboMike

Reputation: 77722

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).

Upvotes: 1

typo.pl
typo.pl

Reputation: 8942

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.

Upvotes: 3

Cristian
Cristian

Reputation: 200080

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

Upvotes: 6

Related Questions