Estou implementando um cadastro com SQLite3 no Android e até aqui tudo bem, o problema está na chamada da Activity
de listagem de dados quando o cadastro é realizado com sucesso, ao fazer a chamada a Activity
a aplicação é fechada com erro.
Segue código:
DevedorActivity
package br.com.savemoney.mastercontas;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import br.com.savemoney.database.DatabaseHelper;
public class DevedorActivity extends Activity {
private DatabaseHelper helper;
private EditText edtNomeDevedor, edtTelefoneDevedor, edtEmailDevedor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.devedor);
edtNomeDevedor = (EditText) findViewById(R.id.edtNomeDevedor);
edtTelefoneDevedor = (EditText) findViewById(R.id.edtTelefoneDevedor);
edtEmailDevedor = (EditText) findViewById(R.id.edtEmailDevedor);
helper = new DatabaseHelper(this);
}
public void cadastrarDevedor(View view) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("nome", edtNomeDevedor.getText().toString());
values.put("telefone", edtTelefoneDevedor.getText().toString());
values.put("email", edtEmailDevedor.getText().toString());
long resultado = db.insert("devedor", null, values);
if(resultado != -1) {
Toast.makeText(getBaseContext(), R.string.cadastro_devedor_sucesso, Toast.LENGTH_LONG).show();
startActivity(new Intent(this, ListaDevedoresActivity.class));
} else {
Toast.makeText(getBaseContext(), R.string.cadastro_devedor_erro, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onDestroy() {
helper.close();
super.onDestroy();
}
}
ListarDevedoresActivity
package br.com.savemoney.mastercontas;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import br.com.savemoney.database.DatabaseHelper;
public class ListaDevedoresActivity extends Activity {
private DatabaseHelper helper;
private SQLiteDatabase db;
private SimpleCursorAdapter adapter;
ListView listViewDevedores;
private static final String[] campos = new String[] {"_id", "nome"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_devedores);
helper = new DatabaseHelper(this);
listViewDevedores = (ListView) findViewById(R.id.listDevedores);
listarDevedores();
}
public void listarDevedores(){
//executa consulta geral de todos os registros cadastrados no banco de dados
Cursor devedores = db.query("agenda", campos, null, null, null, null, null);
if (devedores.getCount() > 0){
//cria cursor que será exibido na tela, nele serão exibidos
//todos os contatos cadastrados
adapter = new SimpleCursorAdapter(this, R.layout.item_devedor, devedores, campos, new int[] { R.id.txtIdDevedor, R.id.txtNomeDevedor});
//relaciona o dataSource ao próprio listview
listViewDevedores.setAdapter(adapter);
}else{
Toast.makeText(this, "Nenhum registro encontrado", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//fecha a conexão com o Banco de dados
db.close();
}
}
LogCat
Aqui, peguei só a parte de erro... Vejam se conseguem ver alguma coisa!
> 06-19 14:21:08.579: E/AndroidRuntime(1971): FATAL EXCEPTION: main
> 06-19 14:21:08.579: E/AndroidRuntime(1971): Process:
> br.com.savemoney.mastercontas, PID: 1971 06-19 14:21:08.579:
> E/AndroidRuntime(1971): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{br.com.savemoney.mastercontas/br.com.savemoney.mastercontas.ListaDevedoresActivity}:
> java.lang.NullPointerException 06-19 14:21:08.579:
> E/AndroidRuntime(1971): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
> 06-19 14:21:08.579: E/AndroidRuntime(1971): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
> 06-19 14:21:08.579: E/AndroidRuntime(1971): at
> android.app.ActivityThread.access$800(ActivityThread.java:135) 06-19
> 14:21:08.579: E/AndroidRuntime(1971): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> 06-19 14:21:08.579: E/AndroidRuntime(1971): at
> android.os.Handler.dispatchMessage(Handler.java:102) 06-19
> 14:21:08.579: E/AndroidRuntime(1971): at
> android.os.Looper.loop(Looper.java:136) 06-19 14:21:08.579:
> E/AndroidRuntime(1971): at
> android.app.ActivityThread.main(ActivityThread.java:5017) 06-19
> 14:21:08.579: E/AndroidRuntime(1971): at
> java.lang.reflect.Method.invokeNative(Native Method) 06-19
> 14:21:08.579: E/AndroidRuntime(1971): at
> java.lang.reflect.Method.invoke(Method.java:515) 06-19 14:21:08.579:
> E/AndroidRuntime(1971): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
> 06-19 14:21:08.579: E/AndroidRuntime(1971): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-19
> 14:21:08.579: E/AndroidRuntime(1971): at
> dalvik.system.NativeStart.main(Native Method) 06-19 14:21:08.579:
> E/AndroidRuntime(1971): Caused by: java.lang.NullPointerException
> 06-19 14:21:08.579: E/AndroidRuntime(1971): at
> br.com.savemoney.mastercontas.ListaDevedoresActivity.listarDevedores(ListaDevedoresActivity.java:35)
> 06-19 14:21:08.579: E/AndroidRuntime(1971): at
> br.com.savemoney.mastercontas.ListaDevedoresActivity.onCreate(ListaDevedoresActivity.java:30)
> 06-19 14:21:08.579: E/AndroidRuntime(1971): at
> android.app.Activity.performCreate(Activity.java:5231) 06-19
> 14:21:08.579: E/AndroidRuntime(1971): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 06-19 14:21:08.579: E/AndroidRuntime(1971): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
> 06-19 14:21:08.579: E/AndroidRuntime(1971): ... 11 more
Alguém consegue ver onde está o erro? De já obrigado.
Cursor devedores = db.query("agenda", campos, null, null, null, null, null);
verifique os valores nesse trecho de código a exception começou na linha 30 e jogou pra 35.