我正在尝试使用this教程创建一个带分区的listView,但我正在关注在logCat中获取此错误:
06-18 13:26:47.538: E/AndroidRuntime(9294): FATAL EXCEPTION: main
06-18 13:26:47.538: E/AndroidRuntime(9294): android.view.InflateException: Binary XML file line #1: Error inflating class include
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-18 13:26:47.538: E/AndroidRuntime(9294): at com.myapp.adapter.EntryAdapter.getView(EntryAdapter.java:37)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.AbsListView.obtainView(AbsListView.java:2040)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.ListView.onMeasure(ListView.java:1155)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.View.measure(View.java:12881)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.View.measure(View.java:12881)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.View.measure(View.java:12881)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.LinearLayout.measureVertical(LinearLayout.java:828)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.View.measure(View.java:12881)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
06-18 13:26:47.538: E/AndroidRuntime(9294): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2111)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.View.measure(View.java:12881)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.os.Looper.loop(Looper.java:137)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.app.ActivityThread.main(ActivityThread.java:4441)
06-18 13:26:47.538: E/AndroidRuntime(9294): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 13:26:47.538: E/AndroidRuntime(9294): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 13:26:47.538: E/AndroidRuntime(9294): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-18 13:26:47.538: E/AndroidRuntime(9294): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-18 13:26:47.538: E/AndroidRuntime(9294): at dalvik.system.NativeStart.main(Native Method)
06-18 13:26:47.538: E/AndroidRuntime(9294): Caused by: java.lang.ClassNotFoundException: android.view.include
06-18 13:26:47.538: E/AndroidRuntime(9294): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-18 13:26:47.538: E/AndroidRuntime(9294): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-18 13:26:47.538: E/AndroidRuntime(9294): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)
06-18 13:26:47.538: E/AndroidRuntime(9294): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
06-18 13:26:47.538: E/AndroidRuntime(9294): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
06-18 13:26:47.538: E/AndroidRuntime(9294): ... 33 more
以下是我在settings_item_head.xml中所拥有的内容(这是错误所在的位置)
<include xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_section_text"
layout="@android:layout/preference_category" />
这是我的适配器:
public class EntryAdapter extends ArrayAdapter {
private Context context;
private ArrayList items;
private LayoutInflater vi;
public EntryAdapter(Context context, ArrayList items) {
super(context, 0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = (Item) items.get(position);
if (i != null) {
if (i.isSection()) {
SectionItem si = (SectionItem) i;
v = vi.inflate(R.layout.settings_item_head, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v
.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
} else {
EntryItem ei = (EntryItem) i;
v = vi.inflate(R.layout.settings_item_item, null);
final TextView title = (TextView) v
.findViewById(R.id.list_item_entry_title);
final TextView subtitle = (TextView) v
.findViewById(R.id.list_item_entry_summary);
if (title != null)
title.setText(ei.title);
if (subtitle != null)
subtitle.setText(ei.subtitle);
}
}
return v;
}
}
答案 0 :(得分:0)
可能不是解决方案,但如果您的帖子不是复制/粘贴问题,那么您的layout.xml有点不对。你有postet:
<include xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_section_text"
layout="@android:layout/preference_category" />
但是,教程看起来像这样:
<linearlayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<include android:id="@+id/list_item_section_text"
layout="@android:layout/preference_category">
</include>
</linearlayout>
因此,如果您的xml帖子是教程中的一部分,则必须删除:
xmlns:android="http://schemas.android.com/apk/res/android"
来自您的包含。这必须放在LinearLayout内。此外,如果这确实是您的错,在更改之后,请清理项目。
答案 1 :(得分:0)
settings_item_head的正确xml布局是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include
android:id="@+id/list_item_section_text"
layout="@android:layout/preference_category" />
</LinearLayout>
这解决了我的问题,答案在教程评论
中