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

时间:2018-02-20 08:06:05

标签: java android

我试图在 RecyclerView 中显示数据,但是应用程序问题错误:

  

尝试调用虚方法' void   android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $布局管理)'   在空对象引用上

RecyclerView声明:

RecyclerView rvTodos;

findViewByid RecyclerView

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

这是我的SetAdapter课程:

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

打开Logcat并显示错误,然后点击错误,然后进入这一行:

rvTodos.setItemAnimator(new DefaultItemAnimator());

这是我的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();
    }
}

我推荐此链接,但它对我不起作用: App crashing when trying to use RecyclerView on android 5.0

What is a NullPointerException, and how do I fix it?

不重复

1 个答案:

答案 0 :(得分:1)

正如您所提到的,您使用了两个recyclerview Id所以请检查您的recyclerview id是否完美id或者您为{{1}分配recyclerview }。

参考代码:

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

prepareToDOData();

内拨打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();
    }