参数类型中的方法setImageBitmap(Bitmap)不适用于参数(int)

时间:2014-04-22 11:02:24

标签: android bitmap android-adapter

制作listAdapter以列出拍摄的照片。我相信这里的一切都还可以。可能是错的? 在这一行得到错误:

adapter.add(new Photo(bitmap , "date"));

ImageView类型中的方法setImageBitmap(Bitmap)不适用于参数(int)

public class Photo {
        public Bitmap icon;
        public String title;
        public Photo(){
            super();
        }

        public Photo(Bitmap bitmap, String title) {
            super();
            this.icon = bitmap;
            this.title = title;
        }

    }


public class PhotoAdapter extends ArrayAdapter<Photo> {

    Context context;
    int layoutResourceId;
    Photo data[] = null;

    public PhotoAdapter(Context context, int layoutResourceId, Photo[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        PhotoHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new PhotoHolder();
            holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
            holder.del_but = (ImageView) row.findViewById(R.id.imgDel);

            row.setTag(holder);
        } else {
            holder = (PhotoHolder) row.getTag();
        }

        Photo photo = data[position];
        holder.txtTitle.setText(photo.title);
        holder.imgIcon.setImageBitmap(photo.icon);

        return row;
    }

    static class PhotoHolder {
        ImageView imgIcon;
        TextView txtTitle;
        ImageView del_but;
    }
}

更新

04-22 14:08:46.113: E/AndroidRuntime(30045): FATAL EXCEPTION: main
04-22 14:08:46.113: E/AndroidRuntime(30045): Process: com.project.simplify, PID: 30045
04-22 14:08:46.113: E/AndroidRuntime(30045): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.project.simplify/com.project.simplify.PhotoUploadActivity}: java.lang.UnsupportedOperationException
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.os.Looper.loop(Looper.java:136)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at java.lang.reflect.Method.invokeNative(Native Method)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at java.lang.reflect.Method.invoke(Method.java:515)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at dalvik.system.NativeStart.main(Native Method)
04-22 14:08:46.113: E/AndroidRuntime(30045): Caused by: java.lang.UnsupportedOperationException
04-22 14:08:46.113: E/AndroidRuntime(30045):    at java.util.AbstractList.add(AbstractList.java:404)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at java.util.AbstractList.add(AbstractList.java:425)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at com.project.simplify.PhotoUploadActivity.setPic(PhotoUploadActivity.java:163)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at com.project.simplify.PhotoUploadActivity.handleBigCameraPhoto(PhotoUploadActivity.java:205)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at com.project.simplify.PhotoUploadActivity.onActivityResult(PhotoUploadActivity.java:251)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
04-22 14:08:46.113: E/AndroidRuntime(30045):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
04-22 14:08:46.113: E/AndroidRuntime(30045):    ... 11 more

2 个答案:

答案 0 :(得分:3)

仅当数据集是Collection,例如ArrayList时,

ArrayAdapter.add才有效。

编辑。发生这种情况是因为将Java数组作为参数的构造通过Arrays.asList将其转换为List。从文档Arrays.asList:

  

返回指定数组中对象的List。的大小   无法修改列表,即不支持添加和删除,但是   元素可以设置。设置元素会修改底层元素   阵列。

Android的代码

  public ArrayAdapter(Context context, int textViewResourceId, T[] objects) {
         init(context, textViewResourceId, 0, Arrays.asList(objects));
  }

编辑你应该改变:

public PhotoAdapter(Context context, int layoutResourceId, Photo[] data)

public PhotoAdapter(Context context, int layoutResourceId, ArrayList<Photo> data)

以及相应的其余代码

答案 1 :(得分:0)

正如您的日志所示,您的错误位于 PhotoUploadActivity.setPic(PhotoUploadActivity.java:163)无论您正在做什么,都会导致您的onActivityResult出现空结果。我认为您将此空结果进入你的Photo.icon成员。

修复该错误,因此您的Photo类包含可在setImageBitmap()中使用的实际位图