当一个 Activity 继承于 ActionBarActivity,并且使用了
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.ActionBar.getThemedContext()' on a null object reference
at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:287)
at android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20)
at android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:98)
at android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71)
at android.app.Activity.onCreatePanelMenu(Activity.java:2921)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:601)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyShortcutEvent(PhoneWindow.java:2450)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4742)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4681)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4188)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4241)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4207)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4344)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4215)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4401)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4188)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4241)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4207)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4215)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4188)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4241)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4207)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4377)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4547)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2331)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1955)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1946)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2308)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:148)
at android.os.Looper.loop(Looper.java:151)
at android.app.ActivityThread.main(ActivityThread.java:5691)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
目前可以确定的是,这是 google support v7 包中 ActionBarActivity 的 bug,以下是这两个 issue 的链接:
问题的原因是,如果一个 Activity 继承于 ActionBarActivity,并且在 theme 中设置
所以,对于网上一个 workaround 的 solution,在 Activity 中屏蔽 MENU 按钮只是一个临时的方案,并不能根本解决这个问题。
解决问题的办法有:
针对 Google 的那两个 issue,最终 Google 也没有真正的解决,从 v7 19.0.1 到 21,直到 22 的时候,ActionBarActivity 被 AppCompatActivity 替代,issue #61394 被设置为 Obsolete。
- EOF -
本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动。
转载请注明:文章转载自 Binkery 技术博客 [https://binkery.com]
本文标题: ActionBarActivity 的子类在获取 ActionBar 的时候出现的空指针
本文地址: https://binkery.com/archives/535.html