2011-11-20 61 views
1

隨時按下後退鍵,我的應用程序崩潰。我嘗試過不同的方式,並且他們都崩潰拋出NullPointerException ....任何想法?Android後退鍵錯誤

方式1:

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    // Check if the key event was the BACK key 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); 
} 

方式2:

public void onBackPressed() { 
     myWebView.goBack(); 
     return; 
} 

堆棧跟蹤:

11-19 20:13:12.425: E/AndroidRuntime(1963): FATAL EXCEPTION: main 
11-19 20:13:12.425: E/AndroidRuntime(1963): java.lang.NullPointerException 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at com.meanwhileinwv.android.MNWVShow.onBackPressed(MNWVShow.java:27) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at android.app.Activity.onKeyUp(Activity.java:1983) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at android.view.KeyEvent.dispatch(KeyEvent.java:1518) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at android.app.Activity.dispatchKeyEvent(Activity.java:2163) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1747) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2702) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2677) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1965) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at android.os.Looper.loop(Looper.java:143) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at android.app.ActivityThread.main(ActivityThread.java:4263) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at java.lang.reflect.Method.invoke(Method.java:507) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
11-19 20:13:12.425: E/AndroidRuntime(1963):  at dalvik.system.NativeStart.main(Native Method) 
+1

你能從崩潰中發佈堆棧跟蹤嗎? NullPointerException發生在哪一行? – goto10

+0

我添加了堆棧跟蹤 – comead

回答

2

當您調用myWebView.goBack()時,獲取您共享的代碼獲取堆棧跟蹤的唯一方法是myWebViewnull。爲了證明這一點,請修改您的onBackPressed()

public void onBackPressed() { 
    if (myWebView != null) { 
     myWebView.goBack(); 
    } 
} 

例外情況應該消失。 (雖然,很明顯,你仍然不會得到任何回來的行爲發生。)

可能的原因:

  • myWebView是不是在你的類的成員變量。您可能已經聲明並將其設置爲另一種方法,這意味着它不在onBackPressed的範圍內。
  • myWebView是不變量的實際名稱是指你的WebView
  • myWebView用來指你的WebView但在某些時候被設置爲null再次
  • ,你認爲是指向myWebView的代碼你WebView實際上設置爲null。例如,findViewById調用實際上找不到WebView,因爲您指定了錯誤的ID將返回null

如果你還在堅持,其中後myWebView聲明,並在那裏被分配的代碼。

1

在該代碼的唯一公共點是myWebView。你確定它不是null?

+0

myWebView正在顯示一個站點,所以它不爲空。它在一個類似的Activity中工作。 – comead

+0

WebView本身可以正常工作,但活動對它的引用仍可能爲空。您是否通過設置斷點來驗證內容? –

+0

我將如何去驗證內容? – comead