简体   繁体   中英

LinearLayout cannot be cast to android.widget.ImageView?

In my listView I am trying to set my imageView so I can show unique images for every row but instead I get this error:-

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)

My CustomAdapter.java class, where I'm assuming the problem is, looks like this:

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;
     }
}

and finally here is my list_row.xml where this image View in question is located:

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

Also when I look at my error log I get the message "Unhandled event loop exception" :

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)

Please help! I can't figure this out.

Well, you're trying to assign a LinearLayout to an ImageView. Instead of:

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

you probably meant:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM