我正在尝试在我的webview应用中创建一个后退按钮。它应该导航webhistory。但是,当我使用后退按钮时,应用程序会一直崩溃。
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(amapps.com.uhss.R.layout.activity_main);
mWebView.setWebViewClient(new WvClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://uhsswordandshield.com/");
mWebView.getSettings().setSupportMultipleWindows(true);
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
// Otherwise defer to system default behavior.
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(amapps.com.uhss.R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == amapps.com.uhss.R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class WvClient extends WebViewClient{
}
}
这是我的Xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity"
>
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
这是我在启动应用时遇到的错误
EXCEPTION: main
Process: amapps.com.uhss, PID: 20498
java.lang.RuntimeException: Unable to start activity ComponentInfo{amapps.com.uhss/amapps.com.uhss.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at amapps.com.uhss.MainActivity.onCreate(MainActivity.java:23)
at andr
如果有人可以请发布一个很好的解决方法。我的应用程序在没有
的情况下完美运行private WebView mWebview
但我无法使用
onBackPressed()
没有它的方法。 我一直在使用Android Studio网站上的代码
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
...
}
}
答案 0 :(得分:0)
您需要在开始调用方法之前指定mWebView :
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WvClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://uhsswordandshield.com/");
mWebView.getSettings().setSupportMultipleWindows(true);
目前,您在分配方法之前正在调用方法,这意味着它在当时将为空。