Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library

I am trying to implement "Flexible Space with Image" from Material Design with the help of this tutorial:

Toolbar animation with android design support library

But I am getting this Rendering problem message in the layout preview :

The following classes could not be instantiated:

- android.support.design.widget.CoordinatorLayout (Open Class, Show Exception, Clear Cache) - android.support.design.widget.AppBarLayout (Open Class, Show Exception, Clear Cache)

I applied the Theme.AppCompat theme to my application but it screws up the action bar and appearance in every other activity. Also it throws an error on action bar methods such as :

actionBar.setDisplayHomeAsUpEnabled(true);

stating the error(roughly) as :

setDisplayHomeAsUpEnabled(boolean) is being called on a null object reference

Additionally using Theme.AppCompat for the entire application gives the following error in the preview screen :

The following classes could not be found: - android.support.v7.internal.app.WindowDecorActionBar (Fix Build Path, Create Class)

Thus I don't want to use Theme.AppCompat for the entire application. However, using a Theme.AppCompat for the specific activity where I want to use the "Flexible space with image" design doesn't resolve the CoordinatorLayout and AppBarLayout issue stated earlier.

Please tell me what to do! I have read many stackoverflow posts on the same issue but they didn't work for me! I have restarted android studio and invalidated my cache and restarted countless times as well!!

Here are the dependencies in my build.gradle

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile project(':viewPagerIndicator')
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

Running the app as is on my phone gives me this error:

java.lang.IllegalStateException: 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.

Setting the windowActionBar to false like so :

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

and then running the app throws this error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seven.actionbar/com.seven.actionbar.EventsDetailActivity}: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }

Here is the toolbar bit from the EventsDetailsActivity.java file :

    Toolbar toolbar;
    CollapsingToolbarLayout collapsingToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eventsdetail);

        actionBarColor =
                new ColorDrawable(ContextCompat.getColor(getApplicationContext(), R.color.ufl_orange));

        tDes = (TextView)findViewById(R.id.evt_desc);
        tVenue = (TextView)findViewById(R.id.evt_venue);
        tDate = (TextView)findViewById(R.id.evt_date);
        tTime = (TextView)findViewById(R.id.evt_time);
        tPdate = (TextView)findViewById(R.id.evt_post_date);
        tPtime = (TextView)findViewById(R.id.evt_post_time);
        tCont = (TextView)findViewById(R.id.evt_contact);
        tOrg = (TextView)findViewById(R.id.evt_org);
        tCount = (TextView)findViewById(R.id.evt_count);

        //Intent intent = getIntent();
        //joinMap = (HashMap)intent.getSerializableExtra("e_uMap");
        myApp = (MyApp)getApplication();

        //action bar magic
        actionBarColor.setAlpha(0);

        toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
        setSupportActionBar(toolbar);
        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        ImageView header = (ImageView) findViewById(R.id.header);

        new LoadDetail().execute();

        goingSwitch = (Switch) findViewById(R.id.btn_join);
        goingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()

        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                new JoinEvents().execute(String.valueOf(isChecked));
            }
        });
    }



    @Override
    public void onStart(){
        super.onStart();
//        actionBar = this.getActionBar();
//        actionBar.setDisplayShowHomeEnabled(false);
    }

Here is the layout code that goes with it:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".EventsDetailActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginBottom="32dp"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/monalisa"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>'

</android.support.design.widget.CoordinatorLayout>

The ScrollView and associated child elements(not shown here) is the main content of the page.

Also modified the theme to:

<style name="EventsTheme" parent="Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

While the IllegalStateException and RuntimeException are gone I can't see the CoordinatorLayout again.

false

like image 848
Flame of udun Avatar asked Nov 16 '15 18:11

Flame of udun


2 Answers

Whats your logcat Throws

java.lang.IllegalStateException: 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.

  1. All of your themes (that want an Action Bar/Toolbar) must inherit from Theme.AppCompat. There are variants available, including Light and NoActionBar.

Please Read Official Document about AppCompat

What should you do

  1. Please set parent="Theme.AppCompat.NoActionBar" instead of parent="Theme.AppCompat.Light"

  2. Avoid Duplicate Library Calling . support:appcompat called two times in your build.gradle section .

You can check similar types of question on SO

  1. Window.FEATURE_ACTION_BAR and set windowActionBar to false
  2. AppCompat does not support the current theme features
like image 96
IntelliJ Amiya Avatar answered Oct 06 '22 00:10

IntelliJ Amiya


OK! the famous Toolbar problem.

As from what it seems, try making few changes in the code you have posted.

1) If you want ActionBar in other activities but this one, create a new style for NoActionBar and add that style to this Activity

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Noe in you AndroidManifest.xml, assign this theme so that only EventsDetailsActivity will not have ActionBar but rest will.

<activity
        android:name="EventsDetailsActivity"
        android:theme="@style/AppTheme.NoActionBar">

2) The ScrollView does not work well with the CoordinatorLayout. So in order to achieve ‘Flexible Space with Image‘ try NestedScrollView instead of ScrollView. This will give you proper animation effect.

3) Change the ActionBar with a Toolbar in your EventsDetailsActivity.

Hope it helps.

like image 45
Pavitra Kansara Avatar answered Oct 06 '22 00:10

Pavitra Kansara