WebView无法访问的代码

时间:2013-09-13 20:21:24

标签: java android browser syntax webview

我正在制作一个WebView浏览器,并且我在MainActivity的一行得到了无法访问的代码错误,我还在学习Android编码,所以如果这是一个noob问题我会道歉,我会粘贴下面的代码。谢谢!

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();
}

}

}

1 个答案:

答案 0 :(得分:1)

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