otnieldocs
otnieldocs

Reputation: 325

getActionBar() return null and requestFeature() appear in ActionBarActivity class

I got some problem when I want to add an application icon at my action bar.

So I have ActionBarActivity class which have these following code in onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_check_stock);

    if(this.getActionBar() != null){
        Log.d("ACTIONBAR_NOT_NULL", "ActionBar Not NULL");
        //getActionBar().setHomeButtonEnabled(true);
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        //getActionBar().setDisplayUseLogoEnabled(true);
        //getActionBar().setDisplayOptions(getActionBar().DISPLAY_SHOW_HOME | getActionBar().DISPLAY_SHOW_TITLE);
        //getActionBar().setIcon(R.drawable.vanwellis_logo);
    }
    else{
        Log.d("ACTIONBAR_NULL","ActionBar NULL");
    }

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

Since my getActionBar() return null, so I add getWindow().requestFeature(Window.FEATURE_ACTION_BAR); But this approach still return an error, but with this new error :

android.util.AndroidRuntimeException: requestFeature() must be called before adding content

So I try to put requestFeature() right before super.onCreate(savedInstanceState);

and the result is...the getActionBar() return null again, but without any errors occurred.

and this is my style.xml I don't know is this applied to all my activities or not, but here you go...

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="drawerArrowStyle">@style/MyDrawerArrowToggle</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:titleTextStyle">@style/MyTitleTextStyle</item>
    <item name="android:background">@color/action_bar_background</item>

    <!-- Support library compatibility -->
    <item name="titleTextStyle">@style/MyTitleTextStyle</item>
    <item name="background">@color/action_bar_background</item>
</style>

<style name="MyDrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@color/foreground</item>
</style>

<style name="MyTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/foreground</item>
</style>

[UPDATE] So this is my whole Activity class

package com.vanwellis.vinnomobile;

import java.util.Locale;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.vanwellis.vinnomobile.fragment.CheckStockFragment;


public class CheckStockActivity extends ActionBarActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_check_stock);

        if(this.getActionBar() != null){
            Log.d("ACTIONBAR_NOT_NULL", "ActionBar Not NULL");
            //getActionBar().setHomeButtonEnabled(true);
            //getActionBar().setDisplayHomeAsUpEnabled(true);
            //getActionBar().setDisplayUseLogoEnabled(true);
            //getActionBar().setDisplayOptions(getActionBar().DISPLAY_SHOW_HOME | getActionBar().DISPLAY_SHOW_TITLE);
            //getActionBar().setIcon(R.drawable.vanwellis_logo);
        }
        else{
            Log.d("ACTIONBAR_NULL","ActionBar NULL");
        }

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_check_stock, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a CheckStockFragment (defined as a static inner class below).
            return CheckStockFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }

}

I'm affraid the Widget.AppCompat.Light.ActionBar.Solid.Inverse will generate no-actionbar.

I'm using android studio with these configuration in my gradle app properties :

android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"

        defaultConfig {
            applicationId "com.vanwellis.vinnomobile"
            minSdkVersion 15
            targetSdkVersion 21
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

So what is really happened in here?

Upvotes: 1

Views: 612

Answers (2)

Bob Liberatore
Bob Liberatore

Reputation: 896

Because your ActionBarActivity is imported from the support library...

import android.support.v7.app.ActionBarActivity;

...you need to use getSupportActionBar() instead of getActionBar().

Upvotes: 1

sourabh devpura
sourabh devpura

Reputation: 625

if you using Theme.AppCompat Theme then you must extends your activity with ActionBarActivity other vise you will get null actionbar

Exp :

public class MyActivity extends ActionBarActivity {

}

Thanks

Upvotes: 0

Related Questions