我正在制作一个 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();
}
}
}