getActionBar() returns null

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

    I had the same issue. It solved by chaning App theme in styles.xml

    Before

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    

    After

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    
    0 讨论(0)
  • 2020-11-22 13:50

    I know I am late to the party (and new to Android) on this question but I found the information here very helpful and thought I should add the findings of my own endeavours with getting ActionBar to work as I wanted in case others like me come looking for help.

    I have a widget which is a floating window with no window title. I use a style theme to implement android:windowIsFloating, android:backgroundDimEnabled and android:windowNoTitle. The widget worked fine until I wanted to add a button that called a fragment pager with several list fragment pages and used the ActionBar. It would crash on the pager activity with a null pointer exception. I narrowed it down to the ActionBar being null. Following the findings of previous people who contributed to this thread I removed my theme from the manifest file and the ActionBar worked fine but now my window now longer floated (it was fullscreen) and it had a page title I did not want.

    Further research took me to the Styles and Themes API Training Guide which led me to a solution. I discovered I could add my custom theme to individual activities in the manifest file whereas before I was applying it to the application. All my windows now have the desired appearance.

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

    Just check the implementation of source code by command click:

        private void initWindowDecorActionBar() {
        Window window = getWindow();
    
        // Initializing the window decor can change window feature flags.
        // Make sure that we have the correct set before performing the test below.
        window.getDecorView();
    
        if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
            return;
        }
    
        mActionBar = new WindowDecorActionBar(this);
        mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
    
        mWindow.setDefaultIcon(mActivityInfo.getIconResource());
        mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
    }
    

    requestWindowFeature(Window.FEATURE_ACTION_BAR); Fixed my issue as I saw requestWindowFeature(Window.FEATURE_ACTION_BAR) is failing; code is open source use it !!

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

    You have to define window type as actionbar before activity render its view.

    use

    requestWindowFeature(Window.FEATURE_ACTION_BAR);
    

    before calling setContentView() method.

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

    In my case I simply had to extend AppCompatActivity instead of Activity

        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    

    Full activity example class:

    import android.os.Bundle import androidx.appcompat.app.AppCompatActivity

    //class LocationFound : Activity() { <-----Does not seem to work with ActionBar in recent versions
    class LocationFound : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_location_found)
    
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        } }
    

    On Versions

        minSdkVersion 22
        targetSdkVersion 29
    
    0 讨论(0)
  • 2020-11-22 13:56

    android.support.v7.app.ActionBar actionBar = getSupportActionBar();

    works pretty quickly

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