Junghyuck Park Junghyuck Park - 1 year ago 71
Android Question

An error occurred when move to before activity to next activity

I have following error when I move from an activity to a new activity.

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

I inputted toolbar in next activity but not in main activity.

I think toolbar causes the error. i don't know where the error is please advice what am i missing


private void redirectMainActivity() {
startActivity(new Intent(this, ServiceActivity.class));


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;

public class ServiceActivity extends AppCompatActivity {

Toolbar mToolbar;
String ClientTAG = "Client";
String OptionTAG = "Option";
private Context context = null;

protected void onCreate(Bundle savedInstanceState) {
mToolbar = (Toolbar) findViewById(R.id.toolBar);
mToolbar.setTitle("tool test");



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

I changed styles.xml and include toolbar.xml to layout_service.xml

Answer Source

As you know, in order to use Toolbar, you must use a theme with windowActionBar set to false. Otherwise, you will get an error like:

This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

Since your first Activity does not have an Toolbar but the second one does, I think you are using same theme for both of them.

However, in Activities that you are using a Toolbar, you must use a Theme which sets <item name="windowActionBar">false</item>

So, I think you can do as follows:

Android Manifest

Set your secondary Activity to use a different theme.


    <activity android:name=".PrimaryActivity" >
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>

        android:theme="@style/AppThemeSecondaryActivity" />


Create a theme with <item name="windowActionBar">false</item> for your secondary activity:

    <style name="AppThemePrimaryActivity" parent="Theme.AppCompat.Light.DarkActionBar">

    <style name="AppThemeSecondaryActivity" parent="AppThemePrimaryActivity">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

This way, you set your secondary activity to always use a theme without default ActionBar and you will be able to set the Toolbar

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download