将数据检索到recyclerview

时间:2019-08-09 21:05:48

标签: android firebase

运行该应用会给出空指针异常

 Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

所以我假设我的RecyclerViewer没有得到数据,不确定是什么原因造成的。

模型类-Images.java

package com.example.alber.undesiredapplication.model;


import com.google.firebase.database.Exclude;

public class Images {

private String image_url2;
private String building_id;
private String mKey;

public Images(String image_url2, String building_id) {
    this.image_url2 = image_url2;
    this.building_id = building_id;
}

public String getImage_url2() {
    return image_url2;
}

public String getBuilding_id() {
    return building_id;
}

public void setImage_url2(String image_url2) {
    this.image_url2 = image_url2;
}

public void setBuilding_id(String building_id) {
    this.building_id = building_id;
}


@Exclude
public String getKey() {
    return mKey;
}

@Exclude
public void setKey(String key) {
    mKey = key;
}
}

RecyclerViewAdapter

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

private Context mContext;
private List<Images> mUploads;


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

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.layout_view_post, parent, false);
    return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    Picasso.get().load(mUploads.get(position).getImage_url2()).fit().centerCrop().into(holder.imageView);
}


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

public class ImageViewHolder extends RecyclerView.ViewHolder{

    public ImageView imageView;
    LinearLayout view_container;

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

        imageView = itemView.findViewById(R.id.thumbnail);
        view_container = itemView.findViewById(R.id.container);
    }
}
}

RecyclerView所在的活动

public class ProfileActivity2 extends AppCompatActivity implements OnMapReadyCallback {


private FloatingActionButton mfloating;
private String b_id;
private RecyclerView mRecyclerview;
private ImagesAdapter mAdapter;
private List<Images> mImages;
private FirebaseStorage mStorage;
private DatabaseReference mDatabaseRef;
private ValueEventListener mDBListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_profile2);

    mfloating = findViewById(R.id.floatingActionButton2);
    mRecyclerview = findViewById(R.id.recycler_view_profile);
    mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
    mAdapter = new ImagesAdapter(ProfileActivity2.this, mImages);
    mRecyclerview.setAdapter(mAdapter);
    mStorage = FirebaseStorage.getInstance();
    mDatabaseRef = 
    FirebaseDatabase.getInstance().getReference("Building_Images");
    mImages = new ArrayList<>();

    mDBListener = mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            mImages.clear();

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Images upload = postSnapshot.getValue(Images.class);
                upload.setKey(postSnapshot.getKey());
                mImages.add(upload);

            }

            mAdapter.notifyDataSetChanged();

        }

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



    mfloating.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String buildingId = b_id;

            Intent startIntent = new Intent(ProfileActivity2.this, ImagesUploadActivity.class);
            startIntent.putExtra("b_id", buildingId);
            startActivity(startIntent);
            finish();
        }
    });



   }

这是我的数据库结构的样子

  "Building_Images" : {
"-Llry4s_9RKTfhEmurMh" : {
  "building_id" : "-LlON58gBZ-LVuEDtZ09",
  "image_url2" : "https://firebasestorage.googleapis.com/v0/b/undesired-project.appspot.com/o/Buildings%..."
},

我在做什么错?我的数据库结构还好吗?

1 个答案:

答案 0 :(得分:1)

mRecyclerview = findViewById(R.id.recycler_view_profile);

findViewById在此行中返回null。确保RecyclerView布局文件中包含recycler_view_profile的{​​{1}}。