在我的listView中我试图设置我的imageView,这样我就可以为每一行显示独特的图像,但我得到了这个错误: -
03-09 17:35:12.338: W/dalvikvm(9208): threadid=1: thread exiting with uncaught exception (group=0x417d5898)
03-09 17:35:12.348: E/AndroidRuntime(9208): FATAL EXCEPTION: main
03-09 17:35:12.348: E/AndroidRuntime(9208): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ImageView
03-09 17:35:12.348: E/AndroidRuntime(9208): at com.example.jsoup.CustomAdapter.getViewOptimize(CustomAdapter.java:41)
03-09 17:35:12.348: E/AndroidRuntime(9208): at com.example.jsoup.CustomAdapter.getView(CustomAdapter.java:27)
03-09 17:35:12.348: E/AndroidRuntime(9208): at android.widget.AbsListView.obtainView(AbsListView.java:2627)
我的CustomAdapter.java类,我假设问题是这样的,如下所示:
public class CustomAdapter extends ArrayAdapter<String> {
Context context;
public CustomAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getViewOptimize(position, convertView, parent);
}
public View getViewOptimize(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//Use an existing view if available otherwise create a new View for passing data to
if (convertView == null) {
LayoutInflater mInflater= (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.mainTitle = (TextView) convertView.findViewById(R.id.title1);
holder.secondTitle= (TextView) convertView.findViewById(R.id.title2);
holder.finTitle= (TextView) convertView.findViewById(R.id.title3);
holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mainTitle.setText(Main.mData.get(position));
holder.secondTitle.setText(Main.mData2.get(position));
holder.finTitle.setText(Main.mData3.get(position));
holder.thumbnail.setImageBitmap(img).get(position);
return convertView;
}
public static class ViewHolder {
TextView mainTitle;
TextView secondTitle;
TextView finTitle;
ImageView thumbnail;
}
}
最后这是我的list_row.xml,其中有问题的视图位于:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="4dip" >
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="60dip"
android:layout_height="60dip"
android:src="@drawable/placeholderimage"/>
</LinearLayout>
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_alignRight="@+id/title3"
android:paddingTop="9dp"
android:layout_toRightOf="@+id/thumbnail"
android:text="title1"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/title2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title1"
android:paddingTop="9dp"
android:textColor="#343434"
android:textSize="10sp"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="title2" />
<TextView
android:id="@+id/title3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/title1"
android:gravity="right"
android:text="title3"
android:layout_marginRight="5dip"
android:textSize="10sp"
android:textColor="#10bcc9"
android:textStyle="bold"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
当我查看我的错误日志时,我收到消息“未处理的事件循环异常”:
java.lang.IllegalArgumentException: Widget has the wrong parent
at org.eclipse.swt.SWT.error(SWT.java:4378)
at org.eclipse.swt.SWT.error(SWT.java:4312)
at org.eclipse.swt.SWT.error(SWT.java:4283)
at org.eclipse.swt.widgets.Widget.error(Widget.java:472)
at org.eclipse.swt.widgets.Control.setMenu(Control.java:3482)
at com.android.ddmuilib.logcat.LogCatPanel$20.handleEvent(LogCatPanel.java:1042)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1081)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1066)
at org.eclipse.swt.widgets.Widget.showMenu(Widget.java:1476)
at org.eclipse.swt.widgets.Widget.wmContextMenu(Widget.java:1578)
at org.eclipse.swt.widgets.Control.WM_CONTEXTMENU(Control.java:4756)
at org.eclipse.swt.widgets.Table.WM_CONTEXTMENU(Table.java:5978)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4615)
at org.eclipse.swt.widgets.Table.windowProc(Table.java:5919)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4990)
at org.eclipse.swt.internal.win32.OS.CallWindowProcW(Native Method)
at org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2443)
at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:564)
at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:430)
at org.eclipse.swt.widgets.Widget.wmRButtonUp(Widget.java:2399)
at org.eclipse.swt.widgets.Control.WM_RBUTTONUP(Control.java:5217)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4679)
at org.eclipse.swt.widgets.Table.windowProc(Table.java:5919)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4990)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2549)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:138)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:610)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:567)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
at org.eclipse.equinox.launcher.Main.main(Main.java:1426)
请帮忙!我无法弄清楚这一点。
答案 0 :(得分:2)
嗯,您正在尝试将LinearLayout分配给ImageView。而不是:
holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail); // oops
你可能意味着:
holder.thumbnail = (ImageView) convertView.findViewById(R.id.list_image);