我刚刚通过本教程创建了一个带有图像的自定义列表,并且进展顺利,(顺便说一句,我将自定义列表的布局更改为线性布局,这是它的代码:
<?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>
问题是我只是在运行应用程序时强制关闭。我知道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