android.support.v7.widget.AppCompatImageView无法强制转换为android.view.ViewGroup

时间:2017-11-19 16:56:45

标签: java android android-recyclerview alertdialog recycler-adapter

我正在通过相机拍照,将它们添加到ArrayList作为位图。我有recyclelerView显示拍摄的图像。我想要一个alertdialog,它显示了带有删除选项的点击图像。我在将自定义布局设置为AlertDialog时遇到问题。当我点击图片时,应用程序崩溃并显示错误

  

java.lang.ClassCastException:android.support.v7.widget.AppCompatImageView无法强制转换为android.view.ViewGroup

错误指向此行代码

  

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

但我不知道该怎么做。我试图将(ViewGroup)改为其他东西,但没有运气。

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>

1 个答案:

答案 0 :(得分:1)

此崩溃背后的原因是您在holder.photo上设置了点击监听器,其类型为AppCompatImageView。在onClick块中,您将其类型转换为ViewGroup

将其更改为以下内容。

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

它应该有用。