最近,尝试从Firebase实时数据库获取数据到RecyclerView
时发生了新错误!
我有一个名为GetReadyListActivity.java
的活动,一个名为GetReadyListModel.java
的模型和一个名为GetReadyViewHolder.java
的ViewHolder。我还有活动的XML,其中包括名为activity_get_ready_list.xml
的RecyclerView和名为recyclerview_get_ready_list.xml
的RecyclerView Item布局。我提供下面的代码!
我认为由于Model在获取状态时包含空对象而导致出现错误(请参阅LogCat)。
Logcat:
2018-12-06 19:06:37.974 19148-19148/com.myapp.something.app W/ClassMapper: No setter/field for 02 found on class com.myapp.something.app.GetReadyListModel
2018-12-06 19:06:37.974 19148-19148/com.myapp.something.app W/ClassMapper: No setter/field for 01 found on class com.myapp.something.app.GetReadyListModel
2018-12-06 19:06:37.974 19148-19148/com.myapp.something.app W/ClassMapper: No setter/field for 03 found on class com.myapp.something.app.GetReadyListModel
2018-12-06 19:06:37.976 19148-19148/com.myapp.something.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.something.app, PID: 19148
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at com.myapp.something.app.GetReadyListViewHolder.setDetails(GetReadyListViewHolder.java:27)
at com.myapp.something.app.GetReadyListActivity$1.populateViewHolder(GetReadyListActivity.java:51)
at com.myapp.something.app.GetReadyListActivity$1.populateViewHolder(GetReadyListActivity.java:48)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:177)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:407)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
at android.view.Choreographer.doCallbacks(Choreographer.java:709)
at android.view.Choreographer.doFrame(Choreographer.java:641)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6361)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
2018-12-06 19:06:37.980 1163-1208/? W/ActivityManager: Force finishing activity com.myapp.something.app/.GetReadyListActivity
GetReadyListModel.java:我的模型类
public class GetReadyListModel {
String title, desc;
Boolean status;
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public GetReadyListModel() {}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
GetReadyListViewHolder.java:“我的视图持有人”类
public class GetReadyListViewHolder extends RecyclerView.ViewHolder {
View mView;
public GetReadyListViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(Context ctx, String title, String desc, Boolean status) {
TextView mTitle = mView.findViewById(R.id.getReadyListTitle);
TextView mDesc = mView.findViewById(R.id.getReadyListDesc);
Switch mStatus = mView.findViewById(R.id.getReadyListStatus);
mTitle.setText(title);
mDesc.setText(desc);
mStatus.setChecked(status);
}
}
GetReadyListActivity.java:包含RecyclerView的活动
public class GetReadyListActivity extends AppCompatActivity {
private Toolbar mToolbar;
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mDatabaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_ready_list);
mToolbar = findViewById(R.id.get_ready_list_app_bar_layout);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("GetReady");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mRecyclerView = findViewById(R.id.getReadyListRecyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mDatabaseReference = mFirebaseDatabase.getReference("tools");
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<GetReadyListModel, GetReadyListViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<GetReadyListModel, GetReadyListViewHolder>(GetReadyListModel.class, R.layout.recyclerview_get_ready_list_row, GetReadyListViewHolder.class, mDatabaseReference) {
@Override
protected void populateViewHolder(GetReadyListViewHolder viewHolder, GetReadyListModel model, int position) {
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDesc(), model.getStatus());
}
@NonNull
@Override
public GetReadyListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_get_ready_list_row, parent, false);
return new GetReadyListViewHolder(view);
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
}
如人们所见:启动活动时将触发java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
。因此,我认为这是模型类的问题,但我无法找到原因。
感谢您的帮助
最高