gpt4 book ai didi

java - WebView 无法访问代码

转载 作者:行者123 更新时间:2023-12-01 14:07:24 26 4
gpt4 key购买 nike

我正在制作一个 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; 语句移至方法末尾。在Java中,从方法返回意味着该方法停止运行。因此,代码 ourBrow = (WebView) findViewById(R.id.wvBrowser); 不可能执行,因为该方法被指示在其之前停止。这会产生编译时错误。

关于java - WebView 无法访问代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18794599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com