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.
对不起我的英文不好。
推荐答案
您需要调用<一个href="http://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#getSupportActionBar%28%29"><$c$c>getSupportActionBar()$c$c>在一个 ActionBarActivity
。不要叫 getActionBar()
- 这不是在旧设备可用,以及 appcompat-V7的新的R21版本
,我希望它返回空
所有的时间,作为新的 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屋!