在自定义listView中交换imageView和textView会使应用程序强制关闭

时间:2013-04-19 11:25:23

标签: android

我刚刚浏览了这个tutorial to create a custom list with images并且进展顺利,(顺便说一下,我将自定义列表的布局更改为linearLayout,这里是代码:

<?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="fill_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

但我想要的是交换imageView(id = icon)和textView(id = title)的位置,就像这样:

<?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="fill_parent"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />


        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

问题是我只是强行关闭运行app.i知道java代码中的convertView是错误的但是我不知道它是什么。谢谢你的帮助

这是我的LogCat:

04-19 12:06:19.121: I/Process(747): Sending signal. PID: 747 SIG: 9
04-19 12:06:27.611: D/AndroidRuntime(773): Shutting down VM
04-19 12:06:27.621: W/dalvikvm(773): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-19 12:06:27.621: E/AndroidRuntime(773): Uncaught handler: thread main exiting due to uncaught exception
04-19 12:06:27.651: E/AndroidRuntime(773): java.lang.ClassCastException: android.widget.ImageView
04-19 12:06:27.651: E/AndroidRuntime(773):  at com.myapp.adapters.CustomListViewAdapter.getView(CustomListViewAdapter.java:43)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.AbsListView.obtainView(AbsListView.java:1274)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.ListView.onMeasure(ListView.java:1060)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.View.measure(View.java:7964)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.View.measure(View.java:7964)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.View.measure(View.java:7964)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.View.measure(View.java:7964)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.View.measure(View.java:7964)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.os.Looper.loop(Looper.java:123)
04-19 12:06:27.651: E/AndroidRuntime(773):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-19 12:06:27.651: E/AndroidRuntime(773):  at java.lang.reflect.Method.invokeNative(Native Method)
04-19 12:06:27.651: E/AndroidRuntime(773):  at java.lang.reflect.Method.invoke(Method.java:521)
04-19 12:06:27.651: E/AndroidRuntime(773):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-19 12:06:27.651: E/AndroidRuntime(773):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-19 12:06:27.651: E/AndroidRuntime(773):  at dalvik.system.NativeStart.main(Native Method)
04-19 12:06:27.671: I/dalvikvm(773): threadid=7: reacting to signal 3
04-19 12:06:28.071: I/dalvikvm(773): Wrote stack trace to '/data/anr/traces.txt'
04-19 12:06:29.661: I/Process(773): Sending signal. PID: 773 SIG: 9

1 个答案:

答案 0 :(得分:1)

问题不是因为布局改变,尝试改变你的代码如下所示只是交换RowItem类中的参数,如

public RowItem(int imageId, String title, String desc) {

public RowItem(String title, int imageId, String desc) {

和ImageTextListViewActivity类

rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
    RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
    rowItems.add(item);
}

rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
    RowItem item = new RowItem(titles[i], images[i], descriptions[i]);
    rowItems.add(item);
}

更新:如果您正在使用eclipse,请删除R.java,重建项目,运行它