getActionBar() returns null

前端 未结 24 1292
暗喜
暗喜 2020-11-22 13:31

I\'m having an odd problem. I am making an app with targetsdk 13.

In my main activity\'s onCreate method i call getActionBar() to setup my actionbar. T

24条回答
  •  情话喂你
    2020-11-22 13:48

    In my case, I had this in my code which did not work:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        context = getApplicationContext();
    
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
    
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
    }
    

    Then I played with the order of the code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
    
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
    
        context = getApplicationContext();
    }
    

    And it worked!

    Conclusion: requestWindowFeature should be the first thing you call in the onCreate method.

提交回复
热议问题