java - android.support.v7.widget.AppCompatImageView 无法转换为 android.view.ViewGroup

标签 java android android-alertdialog android-recyclerview

我正在用相机拍照,并将它们作为位图添加到 ArrayList 中。我有 recyclerView 显示拍摄的图像。我想要一个警报对话框,显示单击的图像以及删除选项。我在将自定义布局设置为 AlertDialog 时遇到问题。当我单击图像时,应用程序崩溃并显示错误

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

错误指向这行代码

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

但我不知道我应该做什么。我尝试将 (ViewGroup) 更改为其他内容,但没有成功。

适配器.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>

最佳答案

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

将其更改为以下。

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

应该可以。

关于java - android.support.v7.widget.AppCompatImageView 无法转换为 android.view.ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47379462/

相关文章:

Android - 如果想要保存他输入的数据,如何在后退键上提醒用户?

java - 有谁知道如何找到 google-api-java-client 的最新 javadocs

android按钮文本和html

android - RecyclerView - 获取 Activity 内部的位置,而不是 RecyclerView 适配器

android - 从警报对话框中删除黑色背景 - Android

android - AlertDialog 单选项填充

java - Hibernate Projection 将结果返回为对象

java - 用于 SSL 客户端身份验证的 Internet Explorer

java - 看不到 JFrame 中的一个组件

android - ruby on rails 中用于 Android 操作系统的 API