使用RecyclerVew的alertdialog

时间:2019-03-05 08:40:13

标签: android android-recyclerview nullpointerexception

我想使用带有回收视图的警报对话框从Firebase存储中检索图像,但是它总是返回消息错误。我可以将图像上传到Firebase存储,但是要上传图像,我希望它检索警报对话框。我仍然对警报对话框适配器感到困惑

这是我在运行应用程序后收到的错误消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference

这是我在DisplayAllActivity中的AlertDialog函数:

private RecyclerView mRecycleView;
private ImageAdapter mAdapter;
private List<Upload> mUpload;

private void showUpdateDialog(){

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

    LayoutInflater inflater = getLayoutInflater();

    final View dialogView = inflater.inflate(R.layout.location_dialog, null);

    dialogBuilder.setView(dialogView);

    mRecycleView = findViewById(R.id.recycler_view);
    mRecycleView.setHasFixedSize(true);
    mRecycleView.setLayoutManager(new LinearLayoutManager(this));

    mUpload = new ArrayList<>();

    databaseReference = FirebaseDatabase.getInstance().getReference("Upload");

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
                Upload upload = postSnapshot.getValue(Upload.class);
                mUpload.add(upload);
            }

            mAdapter = new ImageAdapter(DisplayAllActivity.this, mUpload);

            mRecycleView.setAdapter(mAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(DisplayAllActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

    final AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

}

这是我的ImageAdapter Java类,用于从firebase存储中检索图像

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

private Context mContext;
private List<Upload> mUploads;

public ImageAdapter(Context context, List<Upload> uploads) {
    mContext = context;
    mUploads = uploads;
}

@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.image_location_image, viewGroup, false);
    return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
    Upload uploadCurrent = mUploads.get(i);
    imageViewHolder.textViewName.setText(uploadCurrent.getName());
    Picasso.get().load(uploadCurrent.getImageUrl()).fit().centerCrop().into(imageViewHolder.imageView);
}

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

public class ImageViewHolder extends RecyclerView.ViewHolder {

    public TextView textViewName;
    public ImageView imageView;

    public ImageViewHolder(@NonNull View itemView) {
        super(itemView);

        textViewName = itemView.findViewById(R.id.text_view_name);
        imageView = itemView.findViewById(R.id.image_view_upload);

    }
}}

这是RecycleView的XML,ImageAdapter将使用它来显示Firebase存储中的图像。

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

2 个答案:

答案 0 :(得分:2)

  mRecycleView = findViewById(R.id.recycler_view);

您的回收站视图为空。您必须在已放大的布局中找到视图。

尝试mRecycleView = dialogView.findViewById(R.id.recycler_view);

答案 1 :(得分:1)

通常,NullPointerExceptions最容易识别,只需在日志中搜索该异常,然后您就会看到一些蓝色的文本,然后单击它会将您重定向到引发错误的代码部分。 在这种情况下,问题在于RecyclerView对象无法正确实例化。

只需添加到@Gregory的答案中,您就需要在以编程方式膨胀的布局(image_location_image.xml)中而不是在活动的布局中定义Recyclerview。 然后在dialogView对象上调用findViewById()。