gpt4 book ai didi

java - 尝试使用 Snackbar 时出现 InflateException

转载 作者:行者123 更新时间:2023-12-01 12:35:40 26 4
gpt4 key购买 nike

我有一个 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) 

我目前的类(class)扩展 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();

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

Activity 的 XML:
<?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>

父布局代码:
parentLayout = findViewById(android.R.id.content);
Snackbar在我添加操作栏之前完美运行。

对于这个 Activity ,我使用 Theme.Holo.Light.ActionBar安卓 list :
<activity android:name=".Result"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
android:parentActivityName="com.tripoffbeat.OptionList"/>

在我的 styles.xml我的主题是 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> .

最佳答案

Snackbar膨胀 its content layout , 它cannot inflate a Button 从那个布局,因为它找不到 ?attr/colorAccent从当前语境的主题。发生这种情况,因为您的 Activity 主题不是 AppCompat 的后代主题。

所以,而不是:

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

应用一些 appcompatish 主题。假设您的应用程序的主题是 Theme.AppCompat.Light.DarkActionBar,从 list 中删除该行将解决您的错误。 .

关于java - 尝试使用 Snackbar 时出现 InflateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44749028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com