我有一个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();
如果我使用Toast替换Snackbar消息,它可以正常工作。
活动的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>
父级布局代码:
Parent layout code:
parentLayout = findViewById(android.R.id.content);
在我添加了操作栏之前,Snackbar
很完美地工作。
对于这个活动,我正在使用一个Theme.Holo.Light.ActionBar
的Android清单:
<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">
。