NullPointException with getActionBar

孤人 提交于 2019-12-02 18:22:28

问题


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.

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

My Manifest (Android 3.0 used) :

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

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

Thanks !


回答1:


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



来源:https://stackoverflow.com/questions/8501252/nullpointexception-with-getactionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!