The method setImageBitmap(Bitmap) in the type ImageView is not applicable for the arguments (int)

StackOverflow https://stackoverflow.com/questions/23217623

  •  07-07-2023
  •  | 
  •  

Domanda

making a listAdapter to list a photos taken. i am sure that everything is OK here.what could be wrong?? getting error in this line :

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

The method setImageBitmap(Bitmap) in the type ImageView is not applicable for the arguments (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;
    }
}

UPDATE

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
È stato utile?

Soluzione

ArrayAdapter.add works only if the dataset is a Collection, an ArrayList for instance.

Edit. That happens because that construct that takes a Java array as parameter convert it into a List through Arrays.asList. From the documentation Arrays.asList:

Returns a List of the objects in the specified array. The size of the List cannot be modified, i.e. adding and removing are unsupported, but the elements can be set. Setting an element modifies the underlying array.

Code from Android

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

Edit you should change from:

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

to

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

and the rest of the code accordingly

Altri suggerimenti

As your log indicates, your error is in PhotoUploadActivity.setPic(PhotoUploadActivity.java:163) Whatever you are doing there is causing an empty result in your onActivityResult.I presume you are giving this empty result into your Photo.icon member.

Fix that error, so your Photo class contains an real bitmap that you can use in setImageBitmap()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top