firebase recycler adapter显示空指针异常

时间:2018-05-28 09:22:27

标签: android database firebase adapter

Firebase Recycler视图

    FirebaseRecyclerAdapter<Post, MyViewHolder> fadapter = new FirebaseRecyclerAdapter<Post, MyViewHolder>
            (Post.class, R.layout.card_row, MyViewHolder.class, mDatabase) {
        @Override
        protected void populateViewHolder(MyViewHolder viewHolder, Post model, int position) {

        }
    };

查看持有人类

public static class MyViewHolder extends RecyclerView.ViewHolder {
    View mview;

    public MyViewHolder(View itemView) {
        super(itemView);
        //mview = itemView;

    }

}

Post.java

public class Post {
private String title;
private String image;
private String desc;

public Post(String title, String image, String desc) {
    this.title = title;
    this.image = image;
    this.desc = desc;
}

public Post() {
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

}

我收到此错误!

FATAL EXCEPTION: main
    Process: cf.socialstack.socialstack, PID: 22343                                                                            java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cf.socialstack.socialstack/cf.socialstack.socialstack.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
            at com.firebase.ui.database.FirebaseArray.onCreate(FirebaseArray.java:69)
            at com.firebase.ui.database.ObservableSnapshotArray.addChangeEventListener(ObservableSnapshotArray.java:64)
            at com.firebase.ui.database.FirebaseRecyclerAdapter.startListening(FirebaseRecyclerAdapter.java:126)
            at com.firebase.ui.database.FirebaseRecyclerAdapter.<init>(FirebaseRecyclerAdapter.java:71)
            at com.firebase.ui.database.FirebaseRecyclerAdapter.<init>(FirebaseRecyclerAdapter.java:86)
            at com.firebase.ui.database.FirebaseRecyclerAdapter.<init>(FirebaseRecyclerAdapter.java:108)
            at cf.socialstack.socialstack.MainActivity$2.<init>(MainActivity.java:0)
            at cf.socialstack.socialstack.MainActivity.<init>(MainActivity.java:134)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1572)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) `

3 个答案:

答案 0 :(得分:0)

也许您的DatabaseReference返回null,这就是您获取NullPointException的原因。

您确定您的数据库引用指向一些实际上有一些数据要在回收站视图中填充吗?

答案 1 :(得分:0)

我不知道您想要实现什么,但这是使用实时数据库实现FirebaseRecyclerView的正确方法。

首先创建您的查询以加载数据。

 Query query = FirebaseDatabase.getInstance() .getReference().child("posts").limitToLast(50);

为FirebaseRecyclerView选项创建选项。

FirebaseRecyclerOptions<Post> options = new FirebaseRecyclerOptions.Builder<Post>().setQuery(query, Post.class).build();

创建适配器

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Post, MyViewHolder>(options){
   @Override 
     public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // Create a new instance of the ViewHolder, in this case we are using a custom // layout called R.layout.message for each item 
     View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.message, parent, false);
     return new MyViewHolder(view);
 }

 @Override 
 protected void onBindViewHolder(MyViewHolder holder, int position, Post model) { // Bind the Post object to the MyViewHolder // ...  
   } 
  };

就像使用recyclerView.setAdapter(adapter)将此适配器附加到回收站视图一样简单,并设置布局管理器。

答案 2 :(得分:0)

将您的Recycler适配器放在onCreate方法