gpt4 book ai didi

java - LinearLayout 无法转换为 android.widget.ImageView?

转载 作者:行者123 更新时间:2023-12-02 06:06:00 26 4
gpt4 key购买 nike

在我的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,其中有问题的 ImageView 所在:

<?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)

请帮忙!我无法弄清楚这一点。

最佳答案

好吧,您正在尝试将 LinearLayout 分配给 ImageView。而不是:

holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail); // oops

你的意思可能是:

holder.thumbnail = (ImageView) convertView.findViewById(R.id.list_image);

关于java - LinearLayout 无法转换为 android.widget.ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22288985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com