getAction Bar gives null pointer exception

2019-01-29 03:29发布

i want to work with action bar in android. for this i use

getActionBar().setHomeButtonEnabled(true);

below the SetContentView . but is gives me null pointer exception. when i try only getActionBar() it works fine.

please help me.

2条回答
干净又极端
2楼-- · 2019-01-29 04:28

getActionBar() will return null for pre Honeycomb devices.

If you are targeting Android v2.3 to 4.2 try out ActionBarSherlock.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-29 04:30

If your activity does not have a title bar the method getActionBar() can return null.

You have two options to fix this:

1- Add getWindow().requestFeature(Window.FEATURE_ACTION_BAR); before setContentView

2- In your Manifest specify a Theme that supports ActionBar

Also make sure that you are targeting the right android version e.g. <uses-sdk android:minSdkVersion="11" />

查看更多
登录 后发表回答