尝试使用Snackbar时出现InflateException

时间:2017-06-25 17:52:40

标签: java android xml android-layout android-snackbar

我有一个Snackbar,如果在向服务器请求数据后没有找到任何度假村,则应显示该Snackbar。每次需要显示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)  时,我都会遇到此异常:

ListActivity

我当前的课程延伸ActionBar并正在实施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();

发出错误的行:

<?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>

如果我用Toast替换Snackbar消息,它可以正常工作。

活动的XML:

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

父布局代码:

Snackbar

Theme.Holo.Light.ActionBar在添加操作栏之前完美运行。

对于此活动,我使用<activity android:name=".Result" android:launchMode="singleTop" android:theme="@android:style/Theme.Holo.Light.DarkActionBar" android:parentActivityName="com.tripoffbeat.OptionList"/> Android清单:

styles.xml

在我的<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">我的主题是// You can use some library like "Lodash" to use such functions. // returns random integer in span <min:max> function randomInteger(min = 1, max = 100){ // in case that there are wrong values if(min >= max){throw new Error(`Hey i cant get random integer in range min:${min} to max:${max}`)} return Math.round((Math.random() * (max-min))+min) } // buttons var number1GeneratorButton = document.getElementById('number1-generator'), number2GeneratorButton = document.getElementById('number2-generator'), calculateSumButton = document.getElementById('sum-button'), resetPageButton = document.getElementById('reset-page'); // outputs var number1Output = document.getElementById('number1'), number2Output = document.getElementById('number2'), sumOutput = document.getElementById('sum'); // add event listeners to the buttons number1GeneratorButton.addEventListener('click', function(){ number1Output.innerHTML = randomInteger(); }) number2GeneratorButton.addEventListener('click', function(){ number2Output.innerHTML = randomInteger(); }) calculateSumButton.addEventListener('click', function(){ var sum = parseInt(number1Output.innerHTML) + parseInt(number2Output.innerHTML); sumOutput.innerHTML = sum; }) resetPageButton.addEventListener('click', reloadWindow) function reloadWindow() { window.location.reload(true); } // function addTextNode2(simple3) { // text3 = text2 + text1 // var newtext1 = document.createTextNode(text3), // p3 = document.getElementById("p3"); // p3.appendChild(newtext1); // } // function addTextNode4() { // if (text1 > text2){ // text3 = text1 - text2; // var newtext4 = document.createTextNode(text3), // p3 = document.getElementById("p3");} // else if(text2 > text1){ // text3 = text2 - text1;");} // var newtext4 = document.createTextNode(text3), // p3 = document.getElementById("p3 // p3.appendChild(newtext4); // } // function test(sampleplace1){ // if(text1 > text2){ // q = text1 - text2; // var storm = document.createTextNode(q).value, // p1 = document.getElementById("p1");} // else if(text2 > text1){ // z = text2 - text1; // var storm = document.createTextNode(z).value, // p1 = document.getElementById("p1"); // } // p1.appendChild(storm); // }

1 个答案:

答案 0 :(得分:5)

Snackbarits content layout充气时,cannot inflate a Button会从该布局中消失{{3}},因为它无法从当前上下文的主题中找到?attr/colorAccent。这种情况正在发生,因为您的活动主题不是AppCompat主题的后代。

所以,而不是:

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

应用一些 appcompatish 主题。假设应用程序的主题为Theme.AppCompat.Light.DarkActionBar

,从清单中删除该行将解决您的错误