Quando executo em DEBUG aparece os seguintes erros no console ao chegar nessa parte listaAlunos.setAdapter(adapter); :
E/AndroidRuntime: FATAL EXCEPTION: main Process: br.com.app.zc.agenda, PID: 29918 java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.app.zc.agenda/br.com.app.zc.agenda.ListaAlunosActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2750) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6316) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:344) at android.widget.ListView.setAdapter(ListView.java:502) at br.com.app.zc.agenda.ListaAlunosActivity.onCreate(ListaAlunosActivity.java:44) at android.app.Activity.performCreate(Activity.java:6757) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6316) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'
package br.com.app.zc.agenda;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
import br.com.app.zc.dao.AlunoDAO;
import br.com.app.zc.modelo.Aluno;
public class ListaAlunosActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent nav = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
startActivity(nav);
}
});
AlunoDAO dao = new AlunoDAO(this);
List<Aluno> alunos = dao.buscaAlunos();
dao.close();
ListView listaAlunos = (ListView) findViewById(R.id.lista_alunos);
ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1,alunos);
listaAlunos.setAdapter(adapter);
}
}