简体   繁体   中英

App crashing when trying to use RecyclerView With LayoutManager

I'm trying to show a data in RecyclerView but application crass Error:

Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

Declaration of RecyclerView :

RecyclerView rvTodos;

findViewByid of RecyclerView :

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

Here is my SetAdapter class:

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:

rvTodos.setItemAnimator(new DefaultItemAnimator());

Here is my 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

It's not duplicate with What is a NullPointerException, and how do I fix it?

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 .

Reference Code :

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

call prepareToDOData(); inside 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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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