[英]WebView Unreachable Code
我正在制作WebView浏览器,但在MainActivity的一行中遇到了无法到达的代码错误,我仍在学习Android编码,因此,如果这是一个菜鸟问题,我将在下面粘贴代码表示歉意。 谢谢!
package com.browser.tssomas;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity{
WebView ourBrow;
Button go;
Button back;
Button refresh;
Button forward;
Button clearHistory;
EditText Url;
ProgressBar Pbar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
ourBrow = (WebView) findViewById(R.id.wvBrowser); <-- UNREACHABLE CODE HERE
WebSettings webSettings = ourBrow.getSettings();
webSettings.setJavaScriptEnabled(true);
ourBrow.getSettings().setLoadWithOverviewMode(true);
ourBrow.getSettings().setUseWideViewPort(true);
ourBrow.getSettings().setBuiltInZoomControls(true);
ourBrow.getSettings().setAllowFileAccess(true);
refresh = (Button) findViewById(R.id.bRefresh);
Url = (EditText) findViewById(R.id.etURL);
Pbar = (ProgressBar) findViewById(R.id.progBar);
ourBrow.setWebViewClient(new InsideWebViewClient());
ourBrow.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
}
}
});
{
ourBrow.loadUrl("http://www.google.com");
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.cH:
ourBrow.clearHistory();
return true;
case R.id.HAGAY:
String theWebsite = Url.getText().toString();
if(theWebsite != null)
ourBrow.loadUrl(theWebsite);
return true;
case R.id.Forward:
if (ourBrow.canGoForward())
ourBrow.goForward();
return true;
case R.id.back:
if (ourBrow.canGoBack())
ourBrow.goBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
class myWebClient extends WebViewClient
{
public void refreshButtonClicked(View view)
{
ourBrow.reload();
}
}
}
将return true;
设为return true;
声明到方法结尾。 在Java中,从方法返回意味着该方法停止运行。 因此,代码ourBrow = (WebView) findViewById(R.id.wvBrowser);
是不可能的ourBrow = (WebView) findViewById(R.id.wvBrowser);
永远执行,因为指示该方法在该方法之前停止。 这给出了编译时错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.