向上按钮 Webview 崩溃

Up Button Webview Crashes

我正在尝试在我的网络视图应用程序中创建一个后退按钮。它应该浏览网络历史记录。但是,当我使用后退按钮时,应用程序一直崩溃。

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.access0(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

如果有人可以 post 解决此问题,那就太好了。我的应用程序在没有

的情况下也能完美运行
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) {
   ...
}

}

您需要先分配 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);

目前你在赋值之前正在调用它的方法,这意味着它当时将为空。