Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InflateException when trying to use a Snackbar

I have a Snackbar, which is supposed to show in case no resorts are found after making a request to a server for data. Every time the Snackbar needs to be shown I get this exception:

java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                 at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                 at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                 at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                 at java.lang.Thread.run(Thread.java:818)
                                                              Caused by: android.view.InflateException: Binary XML file line #41: Binary XML file line #41: Error inflating class <unknown>
                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                 at android.support.design.widget.Snackbar.make(Snackbar.java:143)
                                                                 at com.tripoffbeat.Result$LoadAllResorts.doInBackground(Result.java:565)
                                                                 at com.tripoffbeat.Result$LoadAllResorts.doInBackground(Result.java:432)
                                                                 at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                 at java.lang.Thread.run(Thread.java:818) 
                                                              Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
                                                                 at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                                 at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                                                                 at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                 at android.support.design.widget.Snackbar.make(Snackbar.java:143) 
                                                                 at com.tripoffbeat.Result$LoadAllResorts.doInBackground(Result.java:565) 
                                                                 at com.tripoffbeat.Result$LoadAllResorts.doInBackground(Result.java:432) 
                                                                 at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                 at java.lang.Thread.run(Thread.java:818) 
                                                              Caused by: java.lang.reflect.InvocationTargetException
                                                                 at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                 at android.view.LayoutInflater.createView(LayoutInflater.java:619)
                                                                 at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
                                                                 at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                 at android.support.design.widget.Snackbar.make(Snackbar.java:143) 
                                                                 at com.tripoffbeat.Result$LoadAllResorts.doInBackground(Result.java:565) 
                                                                 at com.tripoffbeat.Result$LoadAllResorts.doInBackground(Result.java:432) 
                                                                 at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                 at java.lang.Thread.run(Thread.java:818) 
                                                              Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f0100b0 a=-1}
                                                                 at android.content.res.TypedArray.getColorStateList(TypedArray.java:482)
                                                                 at android.widget.TextView.<init>(TextView.java:1043)
                                                                 at android.widget.Button.<init>(Button.java:109)
                                                                 at android.widget.Button.<init>(Button.java:105)
                                                                 at android.widget.Button.<init>(Button.java:101)
                                                                 at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                                 at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
                                                                 at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
                                                                 at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                 at android.support.design.widget.Snackbar.make(Snackbar.java:143) 
                                                                 at com.tripoffbeat.Result$LoadAllResorts.doInBackground(Result.java:565) 
                                                                 at com.tripoffbeat.Result$LoadAllResorts.doInBackground(Result.java:432) 
                                                                 at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                 at java.lang.Thread.run(Thread.java:818) 

My current class extends ListActivity and is implementing a ActionBar.

The line where it is giving an error:

pDialog.dismiss();
                Snackbar.make(parentLayout, "No resorts found", Snackbar.LENGTH_INDEFINITE)
                        .setAction("CLOSE", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Bundle data = getIntent().getExtras();
                                Intent i = new Intent(Result.this, OptionList.class);
                                i.putExtras(data);
                                setResult(RESULT_OK, getIntent());
                                finish();
                            }
                        })
                        .setActionTextColor(getResources().getColor(android.R.color.holo_blue_dark ))
                        .show();

If I replace the Snackbar message with a Toast it works fine.

XML for the activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1"
app:layout_collapseParallaxMultiplier="1.0"
app:layout_scrollFlags="scroll">

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:text="All resorts and prices"
    android:textColor="@android:color/background_dark"
    android:textSize="20sp" />

<ListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="430dp"
    android:layout_gravity="center_vertical|center_horizontal"
    android:animateLayoutChanges="true"
    android:background="@color/cover_overlay"
    android:choiceMode="multipleChoice"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="false"
    android:outlineProvider="bounds"
    android:paddingBottom="10dp"
    android:paddingEnd="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingStart="10dp"
    android:soundEffectsEnabled="true"
    android:splitMotionEvents="true" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical|center_horizontal"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:paddingTop="7dp"
        android:text="Sort by:"
        android:textColor="@android:color/background_dark"
        android:textSize="16sp" />

    <HorizontalScrollView
        android:layout_width="302dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RadioGroup
                android:id="@+id/rad"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation='horizontal'>

                <RadioButton
                    android:id="@+id/sort_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:checked="true"
                    android:text="Price"
                    android:textColor="@android:color/background_dark" />

                <RadioButton
                    android:id="@+id/sort_alpha"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:checked="false"
                    android:duplicateParentState="false"
                    android:text="Alphabetically"
                    android:textAlignment="center"
                    android:textColor="@android:color/background_dark" />

                <RadioButton
                    android:id="@+id/sort_states"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:checked="false"
                    android:duplicateParentState="false"
                    android:text="State"
                    android:textAlignment="center"
                    android:textColor="@android:color/background_dark" />

                <RadioButton
                    android:id="@+id/sort_city"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Cities" />

                <RadioButton
                    android:id="@+id/sort_dist"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Distance" />

            </RadioGroup>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

Parent layout code:

parentLayout = findViewById(android.R.id.content);

The Snackbar worked perfectly before I added the action bar.

For this activity I'm using a Theme.Holo.Light.ActionBar Android manifest:

<activity android:name=".Result"
        android:launchMode="singleTop"
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
        android:parentActivityName="com.tripoffbeat.OptionList"/>

In my styles.xml my theme is <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">.

like image 712
Dhruv Verma Avatar asked Dec 23 '22 16:12

Dhruv Verma


1 Answers

When Snackbar inflates its content layout, it cannot inflate a Button from that layout, because it lacks to find ?attr/colorAccent from the theme of the current context. That's happening, because the theme of your activity is not a descendant of AppCompat theme.

So, instead of:

android:theme="@android:style/Theme.Holo.Light.DarkActionBar"

apply some appcompatish theme. Removing that line from manifest would resolve your error, assuming the theme of your application is Theme.AppCompat.Light.DarkActionBar.

like image 83
azizbekian Avatar answered Dec 31 '22 00:12

azizbekian