android - 使用自定义布局抛出InflateException导致应用程序强制关闭

时间:2010-07-30 23:14:30

标签: android layout

我正在使用Eclipse并创建了HelloWorld,它使用模拟器工作正常。 然后我尝试创建ToDoList应用程序,您可以在其中输入文本并单击提交,按照我所拥有的书中的示例,并在我与应用程序交互时收到导致我的应用程序强制关闭的错误。

我的目录结构:(请忽略其他文件夹)

ProjectToDoList
   |
   |-----src
   |      |----com.test.dotolist
   |                          |------ToDoList.java
   |                          |------ToDoListItemView.java
   ------res
          |
          |----layout
                 |-------main.xml
                 |-------todolist_item.xml

我追踪它发生的地方:

如果我只是在ToDoList.java中使用默认布局,如:

 final ArrayList<String> todoItems = new ArrayList<String>();
 final ArrayAdapter<String> aa;
 aa = new ArrayAdapter<String>(this, 
            android.R.layout.simple_expandable_list_item_1, todoItems);

该应用程序正常。我可以输入文字并点击按钮以显示文字。

但我正在尝试使用自定义布局:

ToDoList.java,

        final ArrayList<String> todoItems = new ArrayList<String>();
        int resID = R.layout.todolist_item;

        //create array adopter to bind the array to the listview
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(this, resID, todoItems);

        //bind the array adapter to the listview
        myListView.setAdapter(aa);

正如您所看到的,当我尝试将resID传递给ArrayAdapter时,它会编译并且我的应用程序会显示在模拟器上。但是当我点击提交按钮时,它会强行关闭。

todoList_item.xml

<?xml version="1.0" encoding="utf-8"?>
<com.test.todolist.ToDoListItemView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:scrollbars="vertical"
    android:textColor="@color/notepad_text"
    android:fadingEdge="vertical"    
/>

logcat输出:(我无法弄清楚出了什么问题)

W/KeyCharacterMap(  279): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
D/AndroidRuntime(  279): Shutting down VM
W/dalvikvm(  279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  279): FATAL EXCEPTION: main
E/AndroidRuntime(  279): android.view.InflateException: Binary XML file line #2: Error inflating class com.test.todolist.ToDoListItemView
E/AndroidRuntime(  279):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
E/AndroidRuntime(  279):    at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
E/AndroidRuntime(  279):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime(  279):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
E/AndroidRuntime(  279):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
E/AndroidRuntime(  279):    at android.widget.AbsListView.obtainView(AbsListView.java:1315)
E/AndroidRuntime(  279):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
E/AndroidRuntime(  279):    at android.widget.ListView.onMeasure(ListView.java:1109)
E/AndroidRuntime(  279):    at android.view.View.measure(View.java:8171)
E/AndroidRuntime(  279):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
E/AndroidRuntime(  279):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
E/AndroidRuntime(  279):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
E/AndroidRuntime(  279):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
E/AndroidRuntime(  279):    at android.view.View.measure(View.java:8171)
E/AndroidRuntime(  279):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
E/AndroidRuntime(  279):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime(  279):    at android.view.View.measure(View.java:8171)
E/AndroidRuntime(  279):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
E/AndroidRuntime(  279):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
E/AndroidRuntime(  279):    at android.view.View.measure(View.java:8171)
E/AndroidRuntime(  279):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
E/AndroidRuntime(  279):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime(  279):    at android.view.View.measure(View.java:8171)
E/AndroidRuntime(  279):    at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
E/AndroidRuntime(  279):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
E/AndroidRuntime(  279):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  279):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  279):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  279):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  279):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  279):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  279):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  279):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  279): Caused by: java.lang.ClassNotFoundException: com.test.todolist.ToDoListItemView in loader dalvik.system.PathClassLoader[/data/app/com.test.dotolist-1.apk]
E/AndroidRuntime(  279):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
E/AndroidRuntime(  279):    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
E/AndroidRuntime(  279):    at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
E/AndroidRuntime(  279):    at android.view.LayoutInflater.createView(LayoutInflater.java:466)
E/AndroidRuntime(  279):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
E/AndroidRuntime(  279):    ... 32 more
W/ActivityManager(   66):   Force finishing activity com.test.dotolist/.ToDoList

如果您需要澄清,请告诉我。

1 个答案:

答案 0 :(得分:3)

这是你的例外::

Caused by: java.lang.ClassNotFoundException: com.test.todolist.ToDoListItemView in loader dalvik.system.PathClassLoader[/data/app/com.test.dotolist-1.apk]

并查看您的Package ::

ProjectToDoList
   |
   |-----src
   |      |----com.test.dotolist
   |                          |------ToDoListItemView.java

todolist!= dotolist

你有todoList_item.xml

<com.test.todolist.ToDoListItemView

更改为::

<com.test.dotolist.ToDoListItemView

这一定必须奏效! =)