android.widget.ImageView无法强制转换为android.widget.CheckBox

时间:2014-09-10 12:59:49

标签: android checkbox imageview

我的代码崩溃,并且错误图片视图无法转换为checkbok。源代码如下所示。 pl帮我弄清楚pblm

package com.example.application1;


import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import java.io.PrintStream;
import java.util.ArrayList;
//import com.example.application1.R.layout;
import java.util.HashSet;
import android.*;
import com.example.application1.BaseActivity;
import com.example.application1.R.menu;

public class MultiPhotoSelectActivity
  extends BaseActivity
{
  private AlbumAdapter albumAdapter;
  Button done;
  private GridView gridView;
  private ImageAdapter imageAdapter;
  private ArrayList<String> imageUrls;

  private boolean isAlbumView;
  private ArrayList<Album> mAlbumsList;
  private DisplayImageOptions options;


  private void fillAlbums()
  {
    this.isAlbumView = true;
    this.mAlbumsList = new ArrayList();
    String[] arrayOfString = { "_data", "_id", "bucket_display_name", "bucket_id" };
    Cursor localCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, arrayOfString, null, null, null);
    HashSet localHashSet = new HashSet();
    for (int i = 0;; i++)
    {
      if (i >= localCursor.getCount()) {
        return;
      }
      Album localAlbum = new Album();
      localCursor.moveToPosition(i);
      int j = localCursor.getColumnIndex("_data");
      int k = localCursor.getColumnIndex("bucket_display_name");
      int m = localCursor.getColumnIndex("bucket_id");
      localCursor.getColumnIndex("_id");
      String str = localCursor.getString(k);
      localAlbum.setAlbumName(str);
      localAlbum.setImageUrl(localCursor.getString(j));
      localAlbum.albumId = localCursor.getString(m);
      if (localHashSet.add(str)) {
        this.mAlbumsList.add(localAlbum);
      }
      System.out.println("=====> BUCKET_DISPLAY_NAME => " + localCursor.getString(localCursor.getColumnIndex("bucket_display_name")));
    }
  }

  private void fillGallery(String paramString)
  {
    String[] arrayOfString = { "_data", "_id" };
    Cursor localCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, arrayOfString, "bucket_id=?", new String[] { paramString }, "datetaken DESC");
    this.imageUrls = new ArrayList();
    for (int i = 0;; i++)
    {
      if (i >= localCursor.getCount())
      {
        this.imageAdapter = new ImageAdapter(this.imageUrls);
        this.gridView.setAdapter(this.imageAdapter);
        return;
      }
      localCursor.moveToPosition(i);
      int j = localCursor.getColumnIndex("_data");
      this.imageUrls.add(localCursor.getString(j));
    }
  }

  public void onBackPressed()
  {
    ;
    if (this.isAlbumView)
    {
      super.onBackPressed();
      return;
    }
    this.isAlbumView = true;
    this.gridView.setAdapter(this.albumAdapter);
  }

  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));

    //BaseActivity.imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseCont‌​ext()));
   setContentView( com.example.application1.R.layout.activity_multiphoto_);
   fillAlbums();
    this.options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_gallery).showImageForEmptyUri(R.drawable.ic_gallery).cacheInMemory().cacheOnDisc().build();
    this.albumAdapter = new AlbumAdapter(this.mAlbumsList);
    this.gridView = ((GridView)findViewById(R.id.gridview));
    this.done = ((Button)findViewById(R.id.done));
    this.gridView.setAdapter(this.albumAdapter);
    this.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
      public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
      {
        if (MultiPhotoSelectActivity.this.isAlbumView)
        {
          MultiPhotoSelectActivity.this.isAlbumView = false;
          MultiPhotoSelectActivity.this.fillGallery(((MultiPhotoSelectActivity.Album)MultiPhotoSelectActivity.this.mAlbumsList.get(paramAnonymousInt)).albumId);
        }
      }
    });
    this.done.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        ArrayList localArrayList = MultiPhotoSelectActivity.this.imageAdapter.getCheckedItems();
        Intent localIntent = new Intent(MultiPhotoSelectActivity.this, CreateSlideActivity.class);
        localIntent.putExtra("list", localArrayList);
        System.out.println(" " + localArrayList);
        MultiPhotoSelectActivity.this.setResult(-1, localIntent);
        MultiPhotoSelectActivity.this.finish();
      }
    });
  }

  protected void onDestroy()
  {
    super.onDestroy();
  }



  protected void onStop()
  {
    super.onStop();
  }

  public class Album
  {
    public String albumId;
    private String albumName;
    private String imageUrl;

    public Album() {}

    public String getAlbumName()
    {
      return this.albumName;
    }

    public String getImageUrl()
    {
      return this.imageUrl;
    }

    public void setAlbumName(String paramString)
    {
      this.albumName = paramString;
    }

    public void setImageUrl(String paramString)
    {
      this.imageUrl = paramString;
    }
  }

  public class AlbumAdapter
    extends BaseAdapter
  {
    Context mContext;
    ArrayList<MultiPhotoSelectActivity.Album> mList;

    public AlbumAdapter(ArrayList<Album> mAlbumsList)
    {

      this.mList = mAlbumsList;
    }

    public int getCount()
    {
      return this.mList.size();
    }

    public MultiPhotoSelectActivity.Album getItem(int paramInt)
    {
      return (MultiPhotoSelectActivity.Album)this.mList.get(paramInt);
    }

    public long getItemId(int paramInt)
    {
      return paramInt;
    }

    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
    {

      if (paramView == null) {
        paramView = ((LayoutInflater)MultiPhotoSelectActivity.this.getSystemService("layout_inflater")).inflate(R.layout.row_album_item, null);
      }
      final ImageView localImageView = (ImageView)paramView.findViewById(R.id.albumview);
      TextView localTextView = (TextView)paramView.findViewById(R.id.albumName);
      MultiPhotoSelectActivity.this.imageLoader.displayImage("file://" + ((MultiPhotoSelectActivity.Album)this.mList.get(paramInt)).imageUrl, localImageView, MultiPhotoSelectActivity.this.options, new SimpleImageLoadingListener()
      {
        public void onLoadingComplete(Bitmap paramAnonymousBitmap)
        {
          Animation localAnimation = AnimationUtils.loadAnimation(MultiPhotoSelectActivity.this, R.anim.fade_in);
          localImageView.setAnimation(localAnimation);
          localAnimation.start();
        }
      });
      localTextView.setText(((MultiPhotoSelectActivity.Album)this.mList.get(paramInt)).albumName);
      return paramView;
    }
  }

  class ImageAdapter
    extends BaseAdapter
  {
    CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener()
    {
      public void onCheckedChanged(CompoundButton paramAnonymousCompoundButton, boolean paramAnonymousBoolean)
      {
        MultiPhotoSelectActivity.ImageAdapter.this.mSparseBooleanArray.put(((Integer)paramAnonymousCompoundButton.getTag()).intValue(), paramAnonymousBoolean);
      }
    };
    Context mContext;
    ArrayList<String> mList;
    SparseBooleanArray mSparseBooleanArray = new SparseBooleanArray();

    public ImageAdapter(ArrayList<String> imageUrls)
    {

      this.mList = imageUrls;
    }

    public ArrayList<String> getCheckedItems()
    {

        ArrayList<String> arraylist = new ArrayList<String>();
        int i = 0;
        do
        {
            if (i >= mList.size())
            {
                return arraylist;
            }
            if (mSparseBooleanArray.get(i))
            {
                arraylist.add((String)mList.get(i));

            }
            i++;
        } while (true);


    }

    public int getCount()
    {
      return MultiPhotoSelectActivity.this.imageUrls.size();
    }

    public Object getItem(int paramInt)
    {
      return null;
    }

    public long getItemId(int paramInt)
    {
      return paramInt;
    }

    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
    {
        //imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));
      if (paramView == null) {
        paramView = ((LayoutInflater)MultiPhotoSelectActivity.this.getSystemService("layout_inflater")).inflate(R.layout.row_multiphoto_item, null);

      }
      MultiPhotoSelectActivity.this.done.setVisibility(0);
      CheckBox localCheckBox = (CheckBox)paramView.findViewById(R.id.imageView2);
      final ImageView localImageView = (ImageView)paramView.findViewById(R.anim.fade_in);
      MultiPhotoSelectActivity.this.imageLoader.displayImage("file://" + (String)MultiPhotoSelectActivity.this.imageUrls.get(paramInt), localImageView, MultiPhotoSelectActivity.this.options, new SimpleImageLoadingListener()
      {
        public void onLoadingComplete(Bitmap paramAnonymousBitmap)
        {
          Animation localAnimation = AnimationUtils.loadAnimation(MultiPhotoSelectActivity.this, R.anim.fade_in);
          localImageView.setAnimation(localAnimation);
          localAnimation.start();
        }
      });
      localCheckBox.setTag(Integer.valueOf(paramInt));
      localCheckBox.setChecked(this.mSparseBooleanArray.get(paramInt));
      localCheckBox.setOnCheckedChangeListener(this.mCheckedChangeListener);
      return paramView;
    }
  }
}

这是我的xml文件row_multi_photo。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:padding="0.0dip" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout 
        android:background="@drawable/bg_photo" 
        android:padding="3.0dip" 
        android:layout_width="108.0dip" 
        android:layout_height="108.0dip">
        <ImageView 
            android:id="@+id/imageView2" 
            android:layout_width="105.0dip" 
            android:layout_height="105.0dip" 
            android:src="@drawable/ic_launcher" 
            android:scaleType="centerCrop" 
            android:layout_centerInParent="true" />
    </RelativeLayout>
    <CheckBox 
        android:id="@+id/checkBox1" 
        android:padding="3.0dip" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:button="@drawable/checkbox" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentBottom="true" />
</RelativeLayout>

log cat如下所示

   09-10 18:57:51.569: E/AndroidRuntime(25984): FATAL EXCEPTION: main
09-10 18:57:51.569: E/AndroidRuntime(25984): java.lang.IllegalArgumentException: view must not be null
09-10 18:57:51.569: E/AndroidRuntime(25984):    at com.nostra13.universalimageloader.core.imageaware.ViewAware.<init>(ViewAware.java:70)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at com.nostra13.universalimageloader.core.imageaware.ViewAware.<init>(ViewAware.java:50)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at com.nostra13.universalimageloader.core.imageaware.ImageViewAware.<init>(ImageViewAware.java:44)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:365)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:340)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at com.example.application1.MultiPhotoSelectActivity$ImageAdapter.getView(MultiPhotoSelectActivity.java:313)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.widget.AbsListView.obtainView(AbsListView.java:2465)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.widget.GridView.makeAndAddView(GridView.java:1331)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.widget.GridView.makeRow(GridView.java:331)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.widget.GridView.fillDown(GridView.java:283)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.widget.GridView.fillFromTop(GridView.java:407)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.widget.GridView.layoutChildren(GridView.java:1241)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3898)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.View.dispatchTouchEvent(View.java:7234)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2231)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2237)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1979)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2237)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1979)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2237)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1979)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2237)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1979)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.View.dispatchPointerEvent(View.java:7414)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3555)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3487)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4604)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4582)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4686)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:177)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:166)
09-10 18:57:51.569: E/AndroidRuntime(25984):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4654)

3 个答案:

答案 0 :(得分:3)

问题在于:

CheckBox localCheckBox = (CheckBox)paramView.findViewById(R.id.imageView2);

您正在将ImageView转换为CheckBox,将其更改为:

CheckBox localCheckBox = (CheckBox)paramView.findViewById(R.id.checkBox1);

答案 1 :(得分:1)

您曾使用过:

CheckBox localCheckBox = (CheckBox)paramView.findViewById(R.id.imageView2);

改为使用:

CheckBox localCheckBox = (CheckBox)paramView.findViewById(R.id.checkBox1);

答案 2 :(得分:0)

代码中的第311行。您尝试使用错误的ID找到View :-) copy paste error: - )