Zack
Zack

Reputation: 1575

Why am I getting this Exception when I try to remove Action Bar on my Activity?

I am trying to remove ActionBar on my LoginActivity below. I have implemented code from this example code but I do not want the ActionBar to show. I have not altered the code given but I really cannot find how to remove ActionBar(If possible, if not just hide it).

public class LoginActivity extends AppCompatActivity {

// Declaring View and Variables
ViewPager pager;
JoinLoginAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Sign In","Register"};
int Numboftabs = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getSupportActionBar().hide();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.join_login);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new JoinLoginAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);

}

}

I have tried appying NoTheme style to activity with but this gives me the following Exception:

06-28 01:43:45.615  11537-11537/com.nauv.jambomall E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nauv.jambomall/com.nauv.jambomall.ui.activity.JoinLoginActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5414)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:320)
        at com.nauv.jambomall.ui.activity.JoinLoginActivity.onCreate(JoinLoginActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:5369)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5414) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 152

Answers (5)

Abdullah Babu Kalam
Abdullah Babu Kalam

Reputation: 519

Removing the Action Bar

You can hide the action bar at runtime by calling hide().
For example:

  ActionBar actionBar = getActionBar();  
  actionBar.hide();

On API level 11 or higher
Get the ActionBar with the getActionBar() method.

Upvotes: 1

capt.swag
capt.swag

Reputation: 10661

The reason why you are getting AndroidUtilRunTime Exception is you shouldn't be calling requestFeature() after super.onCreate(savedInstanceState). The proper way to do is

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

Other ways to remove ActionBar

ActionBar actionBar = getActionBar();
actionBar.hide();

If you are using android.support.v7.app.ActionBar, then

ActionBar actionBar = getSupportActionBar();
actionBar.hide();

Another way of doing this is, in your styles.xml

<item name="android:windowActionBar">false</item>

Upvotes: 1

Anand Singh
Anand Singh

Reputation: 5692

Instead of:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getSupportActionBar().hide();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.join_login);

Try something like this:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.join_login);

Upvotes: 1

Tchinmai
Tchinmai

Reputation: 620

put this in your styles.xml

<item name="android:windowActionBar">false</item>

and Extend the Activity class instead of AppCompatActivity

Upvotes: 2

adao7000
adao7000

Reputation: 3670

Don't extend the AppCompatActivity. Also go in your xml layout file and remove the Actionbar/Toolbar

Upvotes: 1

Related Questions