的getAction酒吧给空指针异常指针、异常、酒吧、getAction

2023-09-12 03:21:31 作者:墨殇

我想在Android的动作栏中工作。 为了这个,我用

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

getActionBar().setHomeButtonEnabled(true);

在下面的setContentView。但让我空指针异常。 当我尝试只getActionBar(),它工作正常。

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

请帮助我。

推荐答案

如果你的活动不具有标题栏的方法 getActionBar() 可以返回null。

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

您有两个选项来解决这个问题:

You have two options to fix this:

1添加 getWindow()requestFeature(Window.FEATURE_ACTION_BAR); 的setContentView

2-在你的清单指定支持主题动作条

2- In your Manifest specify a Theme that supports ActionBar

另外,还要确保你的目标正确的Andr​​oid版本,例如<使用-SDK安卓的minSdkVersion =11/>

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