0

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

}

}
4

1 回答 1

1

return true;语句移到方法的末尾。在 Java 中,从方法返回意味着该方法停止运行。因此,代码不可能ourBrow = (WebView) findViewById(R.id.wvBrowser);永远执行,因为该方法被指示在它之前停止。这会产生编译时错误。

于 2013-09-13T20:25:54.957 回答