Raddino
Raddino

Reputation: 185

android.support.v7.widget.AppCompatImageView cannot be cast to android.view.ViewGroup

I'm taking pictures by camera, adding them to ArrayList as Bitmap. I have recyclerView that is showing taken images. I want to have an alertdialog, that shows clicked image with option to delete. I'm having a problem with setting custom layout to AlertDialog. When I click on image, app crashes and error says

java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView cannot be cast to android.view.ViewGroup

Error point to this row of code

View viewInflated = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_image, (ViewGroup) view,false); imageDialog.setView(viewInflated);

but I'm not sure what I should do. I tried to change (ViewGroup) to something else but with no luck.

Adapter.java

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

private  List<Bitmap> fotky;
private Bitmap foto;

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.recycler_view_item, parent, false);

    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    foto = fotky.get(position);
    holder.photo.setImageBitmap(foto);
    holder.photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder imageDialog = new AlertDialog.Builder(view.getContext());
            imageDialog.setTitle("Image");
            View viewInflated = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_image, (ViewGroup) view,false);
            imageDialog.setView(viewInflated);
            ImageView image = viewInflated.findViewById(R.id.fotka);
            image.setImageBitmap(foto);
            imageDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();

                }
            });
            imageDialog.setNegativeButton("delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    remove(position);
                }
            });
            imageDialog.show();

            //TODO tady ukázat full image s možností smazání

            //remove(position);
        }



    });

}

public void add(int position, Bitmap item) {
    fotky.add(position, item);
    notifyItemInserted(position);
}

public void remove(int position) {
    fotky.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, fotky.size());
}

public Adapter(List<Bitmap> myDataset) {
    fotky = myDataset;
}


@Override
public int getItemCount() {
    return fotky.size();
}


public class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView photo;

    public ViewHolder(View itemView) {
        super(itemView);
        photo = itemView.findViewById(R.id.imageView);
    }
}

}

Dialog_image.xml

 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fotka"
    android:contentDescription="photo"/>
</LinearLayout>

Upvotes: 0

Views: 8359

Answers (1)

Vipul
Vipul

Reputation: 28093

Reason behind this crash is you have setup click listener on holder.photo which is of type AppCompatImageView. inside onClick block you are typecasting it to ViewGroup.

Change it to following.

View viewInflated = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_image, null);

It should work.

Upvotes: 1

Related Questions