1
I’m implementing a registration with Sqlite3 on Android and so far okay, the problem is in the call of Activity
data listing when the registration is successfully performed, when making the call to Activity
the application is closed with error.
Follows code:
Debtor activity
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
Here, I only got the part of error... See if you can see anything!
> 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
Can anyone see where the mistake is? Thank you.
What error is shown in the log when this happens?
– Luídne
What returns in log Cat? will probably show the line that is in trouble or some error.
– Wellington Avelino
Post below the logcat... no aguardo...
– Francisco André
Cursor devedores = db.query("agenda", campos, null, null, null, null, null);
check the values in this code snippet Exception started on line 30 and played for 35.– Wellington Avelino