getActionBar() returns null

前端 未结 24 1231
暗喜
暗喜 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:46

    This may also help some people.

    In my case, it was because I had not defined a context in the menu.xml

    Try this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.android.ActionBarActivity">
    

    Instead of this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
    0 讨论(0)
  • 2020-11-22 13:47

    I had the same problem and one of the solutions was to use setContentView() before calling getActionBar().

    But there was another thing that fixed the problem. I specified theme for the application to be @android:style/Theme.Holo.Light.

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light" >
        ...
    </application>
    

    I think any theme, which has <item name="android:windowActionBar">true</item> in it, can be used.

    0 讨论(0)
  • 2020-11-22 13:48
    import android.support.v7.app.AppCompatActivity;
    

    then

    extends AppCompatActivity
    

    then use

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 13:49

    go to the AndroidManifest.xml and replace

    android:theme="@style/AppTheme"

    by 
    

    android:theme="@android:style/Theme.Holo.Light.DarkActionBar"

    0 讨论(0)
  • 2020-11-22 13:49

    One thing I wanted to add since I just ran into this, if you are trying to getActionBar() on an Activity that has a parent, it will return null. I am trying to refactor code where my Activity is contained inside an ActivityGroup, and it took a good few minutes for me to go "oh duh" after looking at the source of how an ActionBar gets created in source.

    0 讨论(0)
提交回复
热议问题