Toda vez que vou inserir uma nova nota o app fecha. O console exibe uma nullpointer exception "java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=2, data=Intent { (has extras) }} to activity {br.com.alura.ceep/br.com.alura.ceep.ui.ListaNotasAtivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void br.com.alura.ceep.ui.adapter.RecycleView.ListaNotasAdapter.adiciona(br.com.alura.ceep.model.Nota)' on a null object reference"
Código ListaDeNotasActivity
public class ListaNotasAtivity extends AppCompatActivity {
private ListaNotasAdapter adapter;
private List<Nota> todas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_notas);
List<Nota> todos = notasDeExemplo();
configuraRecyclerView(todos);
TextView textoInsereNota = findViewById(R.id.lista_notas_insere_nota);
textoInsereNota.setOnClickListener(view -> {
Intent abreFormulario = new Intent(ListaNotasAtivity.this, FormularioNotaActivity.class);
startActivityForResult(abreFormulario, 1);
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == 2 && data.hasExtra("nota")){
Nota notaRecebida = (Nota) data.getSerializableExtra("nota");
new NotaDAO().insere(notaRecebida);
adapter.adiciona(notaRecebida);
}
}
@Override
protected void onResume() {
super.onResume();
}
private void configuraRecyclerView(List<Nota> todos) {
RecyclerView lista = findViewById(R.id.RecyclerView);
configuraAdapter(todos, lista);
configuraLayout(lista);
}
private void configuraLayout(RecyclerView lista) {
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
lista.setLayoutManager(staggeredGridLayoutManager);
}
private void configuraAdapter(List<Nota> todos, RecyclerView lista) {
ListaNotasAdapter adapter = new ListaNotasAdapter(this, todos);
lista.setAdapter(adapter);
}
private List<Nota> notasDeExemplo() {
NotaDAO dao = new NotaDAO();
dao.insere(new Nota("Primeira nota", "Olá mundo!!!"));
dao.insere(new Nota("Primeira nota", "Olá mundo555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555!!!"));
dao.insere(new Nota("Primeira nota", "Olá mundo2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222!!!"));
dao.insere(new Nota("Primeira nota", "Olá mundooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!!!"));
return dao.todos();
}
}
Código ListaDeNotasAdapter
public class ListaNotasAdapter extends RecyclerView.Adapter<ListaNotasAdapter.NotaViewHoder> {
private final List<Nota> notas;
private final Context context;
public ListaNotasAdapter(Context context, List<Nota> notas) {
this.context = context;
this.notas = notas;
}
@NonNull
@Override
public NotaViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View viewCriada = LayoutInflater.from(context).inflate(R.layout.item_nota, parent, false);
return new NotaViewHoder(viewCriada);
}
@Override
public void onBindViewHolder(@NonNull NotaViewHoder holder, int position) {
Nota nota = notas.get(position);
holder.vincula(nota);
}
@Override
public int getItemCount() {
return notas.size();
}
public void adiciona(Nota nota) {
notas.add(nota);
notifyDataSetChanged();
}
class NotaViewHoder extends RecyclerView.ViewHolder{
private final TextView titulo;
private final TextView descricao;
public NotaViewHoder(@NonNull View itemView) {
super(itemView);
titulo = itemView.findViewById(R.id.item_nota_titulo);
descricao = itemView.findViewById(R.id.item_nota_descricao);
}
private void vincula(Nota nota) {
titulo.setText(nota.getTitulo());
descricao.setText(nota.getDescricao());
}
}
}