我是初学者,完全失去了尝试解决Inflate Exception的问题。我有一个线索,即Inflate Exception是由bean类的序列化引起的,然而,这一切如何协同工作是我无法理解的。
logcat如下所示,请注意,一旦我打开一个包含ListView的对话框就会发生这种情况,而ListView又基于我所讨论的bean类:
01-31 12:59:22.628 27314-27314/com... I/AppCompatDelegate: The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's
01-31 12:59:22.638 27314-27314/com... D/AndroidRuntime: Shutting down VM
01-31 12:59:26.642 27314-27314/com... W/GAv4: syncDispatchLocalHits timed out: java.util.concurrent.TimeoutException
01-31 12:59:26.647 27314-27314/com... E/AndroidRuntime: FATAL EXCEPTION: main
Process: com..., PID: 27314
android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class item
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com...ui.VideoPickerArrayAdapter.getView(VideoPickerArrayAdapter.java:45)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1281)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18794)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18794)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1191)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class item
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
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.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com...ui.VideoPickerArrayAdapter.getView(VideoPickerArrayAdapter.java:45)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1281)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18794)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18794)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1191)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.item" on path: DexPathList (...)
首先,我有上面提到的bean类Media.java:
public class Media implements Parcelable {
private String duration;
private String title;
private URL url;
private String contentType;
private Bitmap thumbnail;
private String resolution;
private String bandwidth;
public Media(Parcel parcel) throws MalformedURLException {
this.duration = parcel.readString();
this.title = parcel.readString();
this.url = new URL(parcel.readString());
this.contentType = parcel.readString();
this.thumbnail = parcel.readParcelable(Bitmap.class.getClassLoader());
this.resolution = parcel.readString();
this.bandwidth = parcel.readString();
}
/* ... */
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(duration);
dest.writeString(title);
dest.writeString(url.toString());
dest.writeString(contentType);
dest.writeValue(thumbnail);
dest.writeString(resolution);
dest.writeString(bandwidth);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Media createFromParcel(Parcel in) {
try {
return new Media(in);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
}
public Media[] newArray(int size) {
return new Media[size];
}
};
}
此ArrayAdapter中出现Inflate异常:
public class VideoPickerArrayAdapter extends ArrayAdapter<Media> {
public VideoPickerArrayAdapter(Context context, int resource, List<Media> items) {
super(context, resource, items);
}
private class ViewHolder {
ImageView imageView;
TextView txtResolution;
TextView txtDuration;
Button btShare;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final Media media = getItem(position);
LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.video_picker_list_item, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.itemicon);
holder.txtResolution = (TextView) convertView.findViewById(R.id.itemresolution);
holder.txtDuration = (TextView) convertView.findViewById(R.id.itemduration);
holder.btShare = (Button) convertView.findViewById(R.id.ShareButton);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtResolution.setText(media.getResolution() + "p@" + media.getBandwidth() + " Mb/s");
holder.txtDuration.setText(media.getDurationString());
holder.imageView.setImageBitmap(media.getThumbnail());
/* Share button */
holder.btShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(media.getUrl().toString()));
shareIntent.setType("video/mp4");
getContext().startActivity(Intent.createChooser(shareIntent, "Share to"));
}
});
return convertView;
}
listview的XML布局(video_picker_list_item.xml)如下所示,请注意stacktrace中提到的第38行是Share按钮。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/itemicon"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:contentDescription="Videothumbnail"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/itemresolution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="8dp"
android:paddingRight="8dp"
/>
<TextView
android:id="@+id/itemduration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="8dp"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:layout_below="@+id/itemresolution"
android:textAlignment="textEnd"
android:gravity="end"/>
<Button
android:id="@+id/ShareButton"
android:title="Share"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/itemduration"
android:layout_toRightOf="@+id/itemicon"
android:layout_toEndOf="@+id/itemicon"
android:text="Share"/>
</RelativeLayout>
在使Media类Parcelable之前,我不会在每次打开对话框时都获得Inflate异常,但它仍然会像看似随机一样,每次我将一些Media添加到列表中然后打开对话框。我知道,一旦我能够显示列表,膨胀异常就不会发生,即使我关闭并再次打开对话框,除非我改变了它的内容。但是,在我按下分享按钮3秒后,应用程序仍然会崩溃,例外情况是“无法编组Media @ fdsg94934&#39;”。我尝试通过使Media Parcelable来修复此错误,但现在每次尝试打开对话框时我都会遇到Inflate异常。
我希望有经验的Android开发人员能够理解这些东西是如何协同工作的。