当我按下菜单按钮时,我想刷新我的网页视图.这是我的代码:
TuzzaMobileActivity.java
package com.wiseheart.TuzzaMobile; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; public class TuzzaMobileActivity extends Activity { final Activity activity = this; private WebView webView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); WebView webView = (WebView) findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setTitle("Loading..."); activity.setProgress(progress * 100); if(progress == 100) activity.setTitle(R.string.app_name); } }); webView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); webView.loadUrl("http://johnwiseheart.me"); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.quit: finish(); return true; case R.id.refresh: webView.loadUrl( "http://johnwiseheart.me" ); return true; default: return super.onOptionsItemSelected(item); } } }
我已经正确设置了按钮,退出按钮可以工作,但每当我点击我的刷新按钮时,我就会得到一个FC!我做错了什么?
您已定义了一个字段
private WebView webView;
在你的类中,但你然后分配给一个局部变量:
WebView webView = (WebView) findViewById(R.id.webview);
解决方案是将该行更改为
webView = (WebView) findViewById(R.id.webview);