2016-02-05 161 views
-1

在編譯我得到這個錯誤:的Android不兼容類型的錯誤

error: incompatible types: <anonymous WebViewClient> cannot be converted to Context 

錯誤是由該行未來:

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

this,就是要Context類的,我覺得它的東西要做到這一點,我不明白上下文是什麼或如何解決它。

MainActivity.java

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    ProgressDialog progress; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     WebView webView = (WebView) findViewById(R.id.webView); 
     webView.getSettings().setJavaScriptEnabled(true); 

     webView.setWebViewClient(new WebViewClient() { 
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView webView, String urlNewString) { 
       webView.loadUrl("http://www.google.com"); 
       return true; 
      } 
      @Override 
      public void onPageStarted(WebView webView, String url, Bitmap facIcon) { 
       progress = ProgressDialog.show(this, "", "Loading...", true); // the offending line 
      } 
      @Override 
      public void onPageFinished(WebView webView, String url) { 
       drawer.closeDrawer(GravityCompat.START); 
       progress.dismiss(); 
      } 
      public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) { 
       webView.loadUrl("file:///android_asset/www/error.html"); 
       drawer.openDrawer(GravityCompat.START); 
       progress.dismiss(); 
      } 
     }); 
     . 
     . 
    } 
    . 
    . 
} 

回答

5

變化

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

progress = ProgressDialog.show(MainActivity.this, "", "Loading...", true); 

這行代碼是匿名類的onPageStarted方法內。所以this引用了匿名類,而不是當前的活動上下文。

+1

謝謝,這是固定的錯誤。但現在它正在運行,我可以看到代碼有嚴重問題,谷歌網頁無法加載,並且它不再響應android後退按鈕。 – Bazley