Recycle View doesn't show data from adapter [duplicate]

℡╲_俬逩灬. 提交于 2019-12-25 00:59:11

问题


Currently I'm using RecyclerView from Android to show some data that I get from Firebase. I'm using MVC design pattern so I had to pass some data from the view (actViewDB), to the controller(Controller) and then to the Model class (DataModel). Unfortunately I always get the Java NullPointerException error like this

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ryzen.pajakcerdas, PID: 3138
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ryzen.pajakcerdas/com.example.ryzen.pajakcerdas.actViewDB}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ryzen.pajakcerdas.dataAdapter.setNotes(java.util.List)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ryzen.pajakcerdas.dataAdapter.setNotes(java.util.List)' on a null object reference
        at com.example.ryzen.pajakcerdas.actViewDB.showModelList(actViewDB.java:22)

Here is the code

actViewDB.java

public class actViewDB extends AppCompatActivity {

    private List<DataModel> modelList;
    private RecyclerView rc;
    private dataAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.recycle_viewdatabase);

        rc = findViewById(R.id.recycler_view);
        rc.setLayoutManager(new LinearLayoutManager(this));
        rc.setHasFixedSize(true);

        modelList = new ArrayList<>();
    }

    private void showModelList() {
        modelList.add(new DataModel("a", 2, 1, 3));
        adapter.setNotes(modelList);
        rc.setAdapter(adapter);
    }
}

Why is this happening? I'm sorry if this question is already asked, I already search for the answer multiple times from the last week but still can't fix this. The closest problem I found is this android: recycler view adapter but still doesn't fix my problem.


回答1:


You have to initialise the adapter before call its method

adapter = new DataAdapter();


来源:https://stackoverflow.com/questions/56597355/recycle-view-doesnt-show-data-from-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!