recyclerview

i2loujxw  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(306)

最近,尝试将数据从firebase实时数据库获取到 RecyclerView !
我有个活动叫 GetReadyListActivity.java ,一个叫做 GetReadyListModel.java 还有一个叫 GetReadyViewHolder.java . 我还有我的活动的xml,其中包括名为 activity_get_ready_list.xml 我的recyclerview项目布局称为 recyclerview_get_ready_list.xml . 我提供下面的代码!
我认为这个错误是由于模型在获取状态时包含一个空对象而弹出的(参见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 启动活动时被激发。所以我认为这是一个模型类的问题,但我无法找到原因。
感谢您的帮助
最大值

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题