简体   繁体   中英

ProgressDialog is not applicable for the arguments for fragment

I can't use a progress dialog in a Fragment . I tried using a progress dialog in an Activity , however, and it worked.

Here's part of my code:

private class MyCustomWebViewClient extends WebViewClient {
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
          super.onPageStarted(view, url, favicon);
          final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",
               true);
          pd.setCancelable(false);
          pd.setTitle("Please wait");
          pd.setMessage("Page is loading..");
          pd.show();
      }
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          return true;
      }

The error is:

The method show(Context, CharSequence, CharSequence, boolean) in the type
ProgressDialog is not applicable for the arguments 
(CommunityFragment.MyCustomWebViewClient, String, String, boolean)

How can I fix this?

You have this:

final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",
           true);

this is not a valid context. Use ActivityName.this if it's an Activity . If it's a Fragment use CommunityFragment.this.getActivity() .

Change:

final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",
           true);

to:

final ProgressDialog pd = ProgressDialog.show(super.getActivity(), "", "Loading...",
           true);

When using fragments, you need to provide the context of the parent activity.

Try to make a constructor and pass the parent activity as a parameter when you call this class.

Here's an example:

Activity activity;
public MyCustomWebViewClient (Activity _activity) {
    this.activity = _activity;
}

Then use it:

final ProgressDialog pd = ProgressDialog.show(activity, "", "Loading...",
           true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM