这是一个使用简单的android webview
函数的测试应用程序。但是当我按下按钮返回上一个网页时,我在下面遇到了fatal error
;
“java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean android.webkit.WebView.canGoBack()'”
主页(MainActivity.java)
和Web查看页面(WebviewPage.java)
。我试图通过谷歌搜索来修复这个错误...但我不能。
提前致谢。
main activity.Java
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, LoadingActivity.class);
startActivity(intent);}
public void onClick(View view) {
Intent intent1 = new Intent (this, WebViewPage.class);
intent1.putExtra("name","https://Google.com");
startActivity(intent1);
}
}
Web view Page.Java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewPage extends Activity {
Intent intent;
WebView webView;
//key down
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
WebView webView =(WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
intent = getIntent();
String name = intent.getExtras().getString("name");
webView.loadUrl(name);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle(getString(R.string.app_name));
mToolbar.setNavigationIcon(R.drawable.ic_home_black_24dp);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
你在课堂内和'onCreate'里面有两个webView
变量
删除onCreate中的webView变量。
例)
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewPage extends Activity {
Intent intent;
WebView webView;
//key down
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
intent = getIntent();
String name = intent.getExtras().getString("name");
webView.loadUrl(name);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitle(getString(R.string.app_name));
mToolbar.setNavigationIcon(R.drawable.ic_home_black_24dp);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}