2013-11-28 61 views
2

我無法弄清楚爲什麼應用程序一旦按下手機上的後退按鈕就會崩潰。我試着去使用後退按鈕返回一個頁面,而在web視圖在WebView上使用後退按鈕

這是我的代碼:

package com.***.****; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.Window; 
import android.view.WindowManager; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 


public class MainActivity extends Activity { 

WebView myWebView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 

    WebView myWebView = (WebView) findViewById(R.id.webview); 
    myWebView.setWebViewClient(new WebViewClient()); 
    WebSettings webSettings = myWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    myWebView.loadUrl("*****"); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    // Check if the key event was the Back button and if there's history 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { 
     myWebView.goBack(); 
     return true; 
    } 
    // If it wasn't the Back key or there's no web page history, bubble up to the default 
    // system behavior (probably exit the activity) 
    return super.onKeyDown(keyCode, event); 
} 
} 

這是會發生什麼,當我點擊後退按鈕

11-28 14:13:17.768: E/AndroidRuntime(3636): FATAL EXCEPTION: main 
11-28 14:13:17.768: E/AndroidRuntime(3636): java.lang.NullPointerException 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at com.triplec.googledeveloper.MainActivity.onKeyDown(MainActivity.java:35) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at android.view.KeyEvent.dispatch(KeyEvent.java:2756) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at android.app.Activity.dispatchKeyEvent(Activity.java:2428) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2076) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:4192) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:4121) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3169) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at android.os.Looper.loop(Looper.java:137) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at android.app.ActivityThread.main(ActivityThread.java:5328) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at java.lang.reflect.Method.invoke(Method.java:511) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
11-28 14:13:17.768: E/AndroidRuntime(3636):  at dalvik.system.NativeStart.main(Native Method) 
+0

你可以把異常的堆棧跟蹤,使您的應用程序崩潰? – gahfy

+0

我剛剛做了,謝謝 – Crazycriss

+0

看起來像if((keyCode == KeyEvent.KEYCODE_BACK)&& myWebView.canGoBack())崩潰。您需要調試並找出爲什麼myWebView變爲空。 – mbmc

回答

0

這是因爲myWebView爲空。一個快速修復它將是:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if(myWebView != null){ 
     // Check if the key event was the Back button and if there's history 
     if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { 
      myWebView.goBack(); 
      return true; 
     } 
     // If it wasn't the Back key or there's no web page history, bubble up to the default 
     // system behavior (probably exit the activity) 
    } 
    else{ 
     Log.e(MainActivity.class.getSimpleName(), "myWebView is null : won't do anything"); 
    } 
    return super.onKeyDown(keyCode, event); 
} 
+0

您需要在if語句之外調用return super.onKeyDown。 –

+1

完成。感謝您的評論。 – gahfy