带有 getActionBar 的 NullPointException [英] NullPointException with getActionBar

查看:14
本文介绍了带有 getActionBar 的 NullPointException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的平板电脑界面(Android > 3)有一个非常奇怪的行为.当我启动它时,它就像一个底部带有标题的 Android 2* 界面.当然,当我调用 getActionBar() 时,它返回一个 null.

I have a really strange behaviour with my tablet interface (Android > 3). When I launch it, it's like a Android 2* interface with the title at the bottom. And of course when I call the getActionBar() it returns a null.

我的 min-sdk &target-sdk 设置良好,我使用 SDK Android 3.0 (11).我哪里错了?

My min-sdk & target-sdk are well set and I use the SDK Android 3.0 (11). Where am I wrong ?

public class MainScreenActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainlayout);
    ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar));

最后一行出现这个错误:

With the last line I get this error :

Caused by: java.lang.NullPointerException
E/AndroidRuntime( 9082):    at com.shoppingscanner.tablet.MainScreenActivity.onCreate(MainScreenActivity.java:21)
E/AndroidRuntime( 9082):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
E/AndroidRuntime( 9082):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782)
E/AndroidRuntime( 9082):    ... 11 more

我的清单(使用 Android 3.0):

My Manifest (Android 3.0 used) :

<uses-sdk
    android:minSdkVersion="5"
    android:targetSdkVersion="11" />

即使 minSdk 是 11,我也有这种行为.

Even if the minSdk is 11, I have this behaviour.

谢谢!

推荐答案

您还需要 Activity 的窗口来显示标题.你可以从这里

You also need your Activity's window to have the title visible. you can take help from here

这篇关于带有 getActionBar 的 NullPointException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆