简体   繁体   English

尝试将 RecyclerView 与 LayoutManager 一起使用时应用程序崩溃

[英]App crashing when trying to use RecyclerView With LayoutManager

I'm trying to show a data in RecyclerView but application crass Error:我试图在RecyclerView 中显示数据但应用程序错误:

Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference尝试在空对象引用上调用虚拟方法“void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)”

Declaration of RecyclerView : RecyclerView声明:

RecyclerView rvTodos;

findViewByid of RecyclerView : findViewByidRecyclerView

rvTodos = (RecyclerView) findViewById(R.id.rvTodos);

Here is my SetAdapter class:这是我的SetAdapter类:

private void setAdapters() {
        todosAdapter = new TodosAdapter(toDoList);
        RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(getApplicationContext());
        rvTodos.setLayoutManager(mLayoutManager1);
        rvTodos.setItemAnimator(new DefaultItemAnimator());
        rvTodos.setAdapter(todosAdapter);
    }

Open Logcat and show the error and i click on error then it's go in this line:打开 Logcat 并显示错误,然后单击错误,然后在这一行:

rvTodos.setItemAnimator(new DefaultItemAnimator());

Here is my TodosListActivity.class这是我的TodosListActivity.class

public class TodosListActivity extends AppCompatActivity implements View.OnClickListener{

    TextView txtTitle, txtClose, txtSave, txtAdd;

    String label_todos, text_add_todo;

    RecyclerView rvTodos;

    private List<ToDo> toDoList = new ArrayList<>();
    private TodosAdapter todosAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_events_list);

        init();
    }

    private void init() {

        txtTitle = (TextView) findViewById(R.id.txtTitle);
        label_todos = getResources().getString(R.string.label_todos);
        txtTitle.setText(label_todos);

        txtSave = (TextView) findViewById(R.id.txtSave);
        txtSave.setVisibility(View.GONE);

        txtClose = (TextView) findViewById(R.id.txtClose);
        txtClose.setOnClickListener(this);
        txtAdd = (TextView) findViewById(R.id.txtAdd);
        txtAdd.setOnClickListener(this);
        text_add_todo = getResources().getString(R.string.text_add_todo);
        txtAdd.setText(text_add_todo);

        rvTodos = (RecyclerView) findViewById(R.id.rvTodos);

        setAdapters();
        prepareToDOData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.txtAdd:
                Intent intent = new Intent(this, AddTodoActivity.class);
                startActivity(intent);
                break;

            case R.id.txtClose:
                finish();
                break;

        }

    }

    private void setAdapters() {
        todosAdapter = new TodosAdapter(toDoList);
        RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(getApplicationContext());
        rvTodos.setLayoutManager(mLayoutManager1);
        rvTodos.setItemAnimator(new DefaultItemAnimator());
        rvTodos.setAdapter(todosAdapter);
    }

    private void prepareToDOData() {
        ToDo data = new ToDo("David Cummings", "26/10/2017 2:25PM");
        toDoList.add(data);

        data = new ToDo("Lawrence Cummings", "26/10/2017 2:25PM");
        toDoList.add(data);


        data = new ToDo("Ketul Inc.", "11/11/2017 3:46 PM");
        toDoList.add(data);

        //notesAdapter.notifyDataSetChanged();
        todosAdapter.notifyDataSetChanged();
    }
}

I refer this link but it's not work for me : App crashing when trying to use RecyclerView on android 5.0我参考了这个链接,但它对我不起作用: 尝试在 android 5.0 上使用 RecyclerView 时应用程序崩溃

It's not duplicate with What is a NullPointerException, and how do I fix it?它与什么是 NullPointerException不重复, 我该如何解决?

As you mentioned you are using two reyclerView so please check your recyclerview Id is it there perfect recyclerview id or you are assign same id for both of the recyclerview .正如您提到的,您正在使用两个reyclerView因此请检查您的recyclerview Id是否有完美的recyclerview id或者您为两个recyclerview分配了相同的id

Reference Code :参考代码 :

Reference : https://www.androidhive.info/2016/01/android-working-with-recycler-view/参考: https : //www.androidhive.info/2016/01/android-working-with-recycler-view/

call prepareToDOData();调用prepareToDOData(); inside setAdapters()setAdapters()里面

     private void setAdapters() {
        todosAdapter = new TodosAdapter(toDoList);
        RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(getApplicationContext());
        rvTodos.setLayoutManager(mLayoutManager1);
        rvTodos.setItemAnimator(new DefaultItemAnimator());
        rvTodos.setAdapter(todosAdapter);

        prepareToDOData();//call here
    }


 private void prepareToDOData() {
        ToDo data = new ToDo("David Cummings", "26/10/2017 2:25PM");
        toDoList.add(data);

        data = new ToDo("Lawrence Cummings", "26/10/2017 2:25PM");
        toDoList.add(data);


        data = new ToDo("Ketul Inc.", "11/11/2017 3:46 PM");
        toDoList.add(data);

        //notesAdapter.notifyDataSetChanged();
        todosAdapter.notifyDataSetChanged();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM