Estou tentando rodar o meu programa porém quando clico para criar um novo cliente ele está gerando as seguintes mensagens de erro no LogCat:
10-22 11:31:41.575: D/dalvikvm(1250): GC_FOR_ALLOC freed 55K, 4% free 3141K/3272K, paused 93ms, total 94ms
10-22 11:31:41.575: I/dalvikvm-heap(1250): Grow heap (frag case) to 4.208MB for 1127536-byte allocation
10-22 11:31:41.735: D/dalvikvm(1250): GC_FOR_ALLOC freed 2K, 4% free 4240K/4376K, paused 124ms, total 124ms
10-22 11:31:42.485: I/Choreographer(1250): Skipped 36 frames! The application may be doing too much work on its main thread.
10-22 11:31:42.715: D/gralloc_goldfish(1250): Emulator without GPU emulation detected.
10-22 11:31:47.195: D/dalvikvm(1250): GC_FOR_ALLOC freed 30K, 3% free 4713K/4820K, paused 211ms, total 237ms
10-22 11:31:47.605: D/AndroidRuntime(1250): Shutting down VM
10-22 11:31:47.605: W/dalvikvm(1250): threadid=1: thread exiting with uncaught exception (group=0xb1aabba8)
10-22 11:31:47.685: E/AndroidRuntime(1250): FATAL EXCEPTION: main
10-22 11:31:47.685: E/AndroidRuntime(1250): Process: br.com.caelum.cadastro, PID: 1250
10-22 11:31:47.685: E/AndroidRuntime(1250): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.caelum.cadastro/br.com.caelum.cadastro.FormularioActivity}: java.lang.NullPointerException
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.os.Handler.dispatchMessage(Handler.java:102)
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.os.Looper.loop(Looper.java:136)
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-22 11:31:47.685: E/AndroidRuntime(1250): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 11:31:47.685: E/AndroidRuntime(1250): at java.lang.reflect.Method.invoke(Method.java:515)
10-22 11:31:47.685: E/AndroidRuntime(1250): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-22 11:31:47.685: E/AndroidRuntime(1250): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-22 11:31:47.685: E/AndroidRuntime(1250): at dalvik.system.NativeStart.main(Native Method)
10-22 11:31:47.685: E/AndroidRuntime(1250): Caused by: java.lang.NullPointerException
10-22 11:31:47.685: E/AndroidRuntime(1250): at br.com.caelum.cadastro.FormularioHelper.colocaAlunoNoFormulario(FormularioHelper.java:42)
10-22 11:31:47.685: E/AndroidRuntime(1250): at br.com.caelum.cadastro.FormularioActivity.onCreate(FormularioActivity.java:23)
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.app.Activity.performCreate(Activity.java:5231)
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-22 11:31:47.685: E/AndroidRuntime(1250): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-22 11:31:47.685: E/AndroidRuntime(1250): ... 11 more
10-22 11:31:52.945: I/Process(1250): Sending signal. PID: 1250 SIG: 9
Reanalisei o meu AlunoDAO, FormularioHelper, FormularioActivity e também a ListaAlunosActivity, porém não consegui identificar o erro. Alguém poderia me ajudar?
Estou postando todos aqui abaixo:
package br.com.caelum.cadastro.dao;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import br.com.caelum.cadastro.modelo.Aluno;
public class AlunoDAO {
private static final String TABELA = "Aluno";
private final DBHelper helper;
public AlunoDAO(DBHelper helper){
this.helper = helper;
}
private ContentValues toContentValues(Aluno aluno) {
ContentValues values = new ContentValues();
values.put("nome", aluno.getNome());
values.put("telefone", aluno.getTelefone());
values.put("endereco", aluno.getEndereco());
values.put("site", aluno.getSite());
values.put("nota", aluno.getNota());
values.put("caminhoFoto", aluno.getCaminhoFoto());
return values;
}
public void insere(Aluno aluno){
ContentValues cv = toContentValues(aluno);
helper.getWritableDatabase().insert(TABELA, null, cv);
}
public List<Aluno> getLista() {
List<Aluno> alunos = new ArrayList<Aluno>();
String sql = "SELECT * FROM " + TABELA + ";";
Cursor c = helper.getReadableDatabase().rawQuery(sql, null);
while (c.moveToNext()){
Aluno aluno = new Aluno();
aluno.setId(c.getLong(c.getColumnIndex("id")));
aluno.setNome(c.getString(c.getColumnIndex("nome")));
aluno.setEndereco(c.getString(c.getColumnIndex("endereco")));
aluno.setTelefone(c.getString(c.getColumnIndex("telefone")));
aluno.setSite(c.getString(c.getColumnIndex("site")));
aluno.setNota(c.getDouble(c.getColumnIndex("nota")));
aluno.setCaminhoFoto(c.getString(c.getColumnIndex("caminhoFoto")));
alunos.add(aluno);
}
return alunos;
}
public void deletar(Aluno aluno) {
String[] args = {aluno.getId().toString()};
helper.getWritableDatabase().delete(TABELA, "id=?", args);
}
public void atualizar(Aluno aluno) {
ContentValues cv = toContentValues(aluno);
String[] args = {aluno.getId().toString()};
helper.getWritableDatabase().update(TABELA, cv, "id=?", args);
}
}
package br.com.caelum.cadastro;
import android.widget.EditText;
import android.widget.SeekBar;
import br.com.caelum.cadastro.modelo.Aluno;
public class FormularioHelper {
private EditText campoNome;
private EditText campoEndereco;
private EditText campoSite;
private EditText campoTelefone;
private SeekBar campoNota;
private Aluno aluno;
public FormularioHelper(FormularioActivity activity) {
aluno = new Aluno();
campoNome = (EditText) activity.findViewById(R.id.nome);
campoEndereco = (EditText) activity.findViewById(R.id.endereco);
campoSite = (EditText) activity.findViewById(R.id.site);
campoTelefone = (EditText) activity.findViewById(R.id.telefone);
campoNota = (SeekBar) activity.findViewById(R.id.nota);
}
public Aluno pegaAlunoDoFormulario(){
String nome = campoNome.getText().toString();
String endereco = campoEndereco.getText().toString();
String telefone = campoTelefone.getText().toString();
String site = campoSite.getText().toString();
int nota = campoNota.getProgress();
aluno.setNome(nome);
aluno.setEndereco(endereco);
aluno.setSite(site);
aluno.setTelefone(telefone);
aluno.setNota(Double.valueOf(nota));
return aluno;
}
public void colocaAlunoNoFormulario(Aluno alunoParaSerAlterado) {
campoNome.setText(alunoParaSerAlterado.getNome());
campoEndereco.setText(alunoParaSerAlterado.getEndereco());
campoSite.setText(alunoParaSerAlterado.getSite());
campoTelefone.setText(alunoParaSerAlterado.getTelefone());
campoNota.setProgress(alunoParaSerAlterado.getNota().intValue());
}
}
package br.com.caelum.cadastro;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import br.com.caelum.cadastro.dao.AlunoDAO;
import br.com.caelum.cadastro.dao.DBHelper;
import br.com.caelum.cadastro.modelo.Aluno;
public class FormularioActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.formulario);
final Button botao = (Button) findViewById(R.id.btngravar);
final FormularioHelper helper = new FormularioHelper(this);
final Aluno alunoParaSerAlterado = (Aluno) getIntent().getSerializableExtra("alunoSelecionado");
final DBHelper db = new DBHelper(this);
helper.colocaAlunoNoFormulario(alunoParaSerAlterado);
Toast.makeText(this, "Aluno: " + alunoParaSerAlterado, Toast.LENGTH_SHORT).show();
botao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(FormularioActivity.this, "Você clicou no Botão", Toast.LENGTH_LONG).show();
Aluno aluno = helper.pegaAlunoDoFormulario();
AlunoDAO dao = new AlunoDAO(db);
if (alunoParaSerAlterado != null) {
aluno.setId(alunoParaSerAlterado.getId());
botao.setText("Alterar");
dao.atualizar(aluno);
} else {
dao.insere(aluno);
}
db.close();
finish();
}
});
}
}
package br.com.caelum.cadastro;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import br.com.caelum.cadastro.dao.AlunoDAO;
import br.com.caelum.cadastro.dao.DBHelper;
import br.com.caelum.cadastro.modelo.Aluno;
public class ListaAlunosActivity extends Activity {
private ListView lista;
private Aluno aluno;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listagem_alunos);
lista = (ListView) findViewById(R.id.lista);
registerForContextMenu(lista);
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id) {
Aluno alunoParaSerAlterado = (Aluno) adapter.getItemAtPosition(posicao);
Intent irParaFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
irParaFormulario.putExtra("alunoSelecionado", alunoParaSerAlterado);
startActivity(irParaFormulario);
}
});
lista.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View view, int posicao, long id) {
aluno = (Aluno) adapter.getItemAtPosition(posicao);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_lista_alunos, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.novo:
Intent irParaFormulario = new Intent(this, FormularioActivity.class);
startActivity(irParaFormulario);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
protected void carregaLista() {
DBHelper db = new DBHelper(this);
AlunoDAO dao = new AlunoDAO(db);
List<Aluno> alunos = dao.getLista();
ArrayAdapter<Aluno> adapter =
new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, alunos);
lista.setAdapter(adapter);
db.close();
}
@Override
protected void onResume() {
super.onResume();
this.carregaLista();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
MenuItem ligar = menu.add("Ligar");
ligar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent irParaATelaDeDiscagem = new Intent(Intent.ACTION_CALL);
Uri telefoneDoAluno = Uri.parse("tel:" + aluno.getTelefone());
irParaATelaDeDiscagem.setData(telefoneDoAluno);
return false;
}
});
MenuItem sms = menu.add("Enviar SMS");
sms.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent enviarSMS = new Intent(Intent.ACTION_VIEW);
Uri telefoneDoAluno = Uri.parse("sms:" + aluno.getTelefone());
enviarSMS.setData(telefoneDoAluno);
enviarSMS.putExtra("sms_body", "Mensagem para o Aluno " + aluno.getNome().toString());
return false;
}
});
MenuItem mapa = menu.add("Achar no Mapa");
mapa.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent acharNoMapa = new Intent(Intent.ACTION_VIEW);
Uri enderecoDoAluno = Uri.parse("geo:0,0?z=14&q" + aluno.getEndereco());
acharNoMapa.setData(enderecoDoAluno);
return false;
}
});
MenuItem site = menu.add("Navegar no Site");
site.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent abrirOSiteDoAluno = new Intent(Intent.ACTION_VIEW);
Uri siteDoAluno = Uri.parse("http://" + aluno.getSite());
abrirOSiteDoAluno.setData(siteDoAluno);
return false;
}
});
MenuItem deletar = menu.add("Deletar");
deletar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
DBHelper db = new DBHelper(ListaAlunosActivity.this);
AlunoDAO dao = new AlunoDAO(db);
dao.deletar(aluno);
db.close();
carregaLista();
return false;
}
});
menu.add("Enviar E-mail");
super.onCreateContextMenu(menu, v, menuInfo);
}
}