0

I want to retrieve image from firebase store using the alert dialog with recycle view but it always returns a message error. I can upload an image to firebase store, but to upload it I want it to retrieve alert dialog. I still confusing about the adapter for alert dialog

This is the error message that I get after run the application:

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

This is my AlertDialog function in DisplayAllActivity:

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();

}

Here is my ImageAdapter java class for the image to be retrieved form firebase store

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);

    }
}}

This is XML for RecycleView that will be used by the ImageAdapter to display the image from firebase store.

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

2
  mRecycleView = findViewById(R.id.recycler_view);

Your recycler view is null. You have to find the view in the layout you have inflated.

Try mRecycleView = dialogView.findViewById(R.id.recycler_view);

1

Usually NullPointerExceptions are easiest to identify, just search for the exception in log, and you have some text in blue color, and clicking on it redirects you to the part of code where the error is being thrown. In this case, the problem is with the RecyclerView object not being instantiated properly.

Just to add to @Gregory's answer, you need to have the Recyclerview defined in the layout(image_location_image.xml) that you have inflated programmatically, not in the activity's layout. Then call the findViewById() on the dialogView object.

Harsha Vardhan
  • 446
  • 4
  • 15