getActionBar() 返回 Null (AppCompat-v7 21) [英] getActionBar() returns Null (AppCompat-v7 21)
问题描述
在我将 AppCompat-v7 更改为 21.0.0 并且编译没有问题后,我的应用程序在运行的那一刻崩溃.
My app is crashing the minute I run it after I changed my AppCompat-v7 to 21.0.0 and Compiled with no problem.
它给了我错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setIcon(int)' on a null object reference
在这一行:
getActionBar().setIcon(R.drawable.ic_action_bar);
它适用于 AppCompat-v7 20.0.0,但不适用于 21.0.0.
It works with AppCompat-v7 20.0.0, but not with 21.0.0.
推荐答案
需要调用getSupportActionBar()
在 ActionBarActivity
上.不要调用 getActionBar()
-- 这在旧设备上不可用,对于 appcompat-v7
的新 r21 版本,我希望它返回 null
一直为空,因为新的 ActionBarActivity
禁用并替换了系统操作栏.
You need to call getSupportActionBar()
on an ActionBarActivity
. Do not call getActionBar()
-- that is not available on older devices, and for the new r21 edition of appcompat-v7
, I would expect it to return null
all the time, as the new ActionBarActivity
disables and replaces the system action bar.
这篇关于getActionBar() 返回 Null (AppCompat-v7 21)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!