编译后我收到此错误:
error: incompatible types: <anonymous WebViewClient> cannot be converted to Context
错误来自这一行:
progress = ProgressDialog.show(this, "", "Loading...", true);
this
应该是班级Context
,我认为这与此有关,我不了解上下文是什么或如何解决它。< / p>
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();
}
});
.
.
}
.
.
}
答案 0 :(得分:5)
更改
progress = ProgressDialog.show(this, "", "Loading...", true);
到
progress = ProgressDialog.show(MainActivity.this, "", "Loading...", true);
该行代码位于匿名类的onPageStarted
方法内。所以this
指的是匿名类,而不是当前的活动上下文。