向CursorAdapter充气时出错

时间:2016-02-29 16:02:20

标签: android

这是我的代码:

public class ConversationListAdapter extends CursorAdapter
        implements AbsListView.RecyclerListener {
    private final LayoutInflater inflater;
    Cursor cursor = null;

    public ConversationListAdapter(Context context, Cursor cursor) {
        super(context, cursor, false);
        this.cursor = cursor;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Conversation conv = Conversation.from(context, cursor);
        ConversationListItem convView = (ConversationListItem) view;
        convView.bind(conv);
    }

    @Override
    public void onMovedToScrapHeap(View view) {
        ConversationListItem cli = (ConversationListItem) view;
        cli.unbind();
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return inflater.inflate(R.layout.conversation_list_item, parent, false);// <- line 43
    }
}

这是logcat:

android.view.InflateException: Binary XML file line #2: Error inflating class com.test.conversation.ConversationListItem
                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                             **at com.example.daniel.conversation.ConversationListAdapter.newView(ConversationListAdapter.java:43)**
                                                             at android.widget.CursorAdapter.getView(CursorAdapter.java:250)
                                                             at android.widget.AbsListView.obtainView(AbsListView.java:2387)
                                                             at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
                                                             at android.widget.ListView.onMeasure(ListView.java:1182)
                                                             at android.view.View.measure(View.java:17496)
                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
                                                             at android.view.View.measure(View.java:17496)
                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                             at android.view.View.measure(View.java:17496)
                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438)
                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:724)
                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
                                                             at android.view.View.measure(View.java:17496)
                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                             at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2636)
                                                             at android.view.View.measure(View.java:17496)
                                                             at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031)
                                                             at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1193)
                                                             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
                                                             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
                                                             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5875)
                                                             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                             at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                             at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                             at android.os.Looper.loop(Looper.java:135)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                                                          Caused by: java.lang.ClassNotFoundException: Didn't find class "com.test.conversation.ConversationListItem" on path: DexPathList[[zip file "/data/app/com.example.daniel.sms-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
                                                             at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                             at android.view.LayoutInflater.createView(LayoutInflater.java:571)
                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                             **at com.example.daniel.conversation.ConversationListAdapter.newView(ConversationListAdapter.java:43)** 
                                                             at android.widget.CursorAdapter.getView(CursorAdapter.java:250) 
                                                             at android.widget.AbsListView.obtainView(AbsListView.java:2387) 
                                                             at android.widget.ListView.measureHeightOfChildren(ListView.java:1270) 
                                                             at android.widget.ListView.onMeasure(ListView.java:1182) 
                                                             at android.view.View.measure(View.java:17496) 
                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464) 
                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438) 
                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:724) 
                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:615) 
                                                             at android.view.View.measure(View.java:17496) 
                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464) 
                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
                                                             at android.view.View.measure(View.java:17496) 
                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464) 
                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1438) 
                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:724) 
                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:615) 
                                                             at android.view.View.measure(View.java:17496) 
                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464) 
                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
                                                             at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2636) 
                                                             at android.view.View.measure(View.java:17496) 
                                                             at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031) 
                                                             at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1193) 
                                                             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400) 
                                                             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078) 
                                                             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5875) 
                                                             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
                                                             at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
                                                             at android.view.Choreographer.doFrame(Choreographer.java:550) 
                                                             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
                                                             at android.os.Handler.handleCallback(Handler.java:739) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                             at android.os.Looper.loop(Looper.java:135) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:5299) 
                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 
                                                            Suppressed: java.lang.ClassNotFoundException: com.test.conversation.ConversationListItem
                                                             at java.lang.Class.classForName(Native Method)
                                                             at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                             at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                    ... 45 more
                                                          Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no s

inflater.inflate(R.layout.conversation_list_item, parent, false)似乎有问题,当我没有在清单中放置任何<user-permission>时,APP就可以成功启动。

有人可以帮我这个吗?好几天我真的很困惑!提前谢谢。

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<com.test.conversation.ConversationListItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:paddingRight="10dip" >

    <QuickContactBadge
        android:id="@+id/contact_avatar"
        android:layout_marginLeft="7dip"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_contact_picture"
        style="?android:attr/quickContactBadgeStyleWindowSmall" />

    <TextView
        android:id="@+id/thread_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"/>

    <TextView android:id="@+id/from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:layout_marginTop="6dip"
        android:layout_marginRight="5dip"
        android:layout_marginLeft="7dip"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/contact_avatar"
        android:layout_alignWithParentIfMissing="true"
        android:ellipsize="marquee"
        android:textSize="20dip"  />

    <TextView android:id="@+id/snippet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="7dip"
        android:layout_below="@id/from"
        android:layout_toRightOf="@id/contact_avatar"
        android:layout_toLeftOf="@+id/date"
        android:layout_alignParentBottom="true"
        android:layout_alignWithParentIfMissing="true"
        android:ellipsize="end"
        android:textSize="12dip" />

    <TextView android:id="@+id/date"
        android:layout_marginTop="2dip"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="5dip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:singleLine="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:textSize="12dip"/>

</com.test.conversation.ConversationListItem>

1 个答案:

答案 0 :(得分:1)

很明显。您有一个自定义视图,您在XML中调用它:com.test.conversation.ConversationListItem但它无法找到此类。确保这是此类的写入名称和包。或者如果它存在于您的项目中。