Listview of Android database - Close application with error

Asked

Viewed 583 times

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?

  • What returns in log Cat? will probably show the line that is in trouble or some error.

  • Post below the logcat... no aguardo...

  • 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.

1 answer

1

Is a Nullpointerexception, that is, there is a variable with null value.

The best way to solve runtime errors, as is the case with Nullpointerexception, is debugging the code.

Search the error stack for references to your source files and lines that caused the error.

For example: Exemploactivity:96.

Place a stop point before the given line (96), run the debug and Inspecione the values of your variables, step by step.

It is not worth to assume what happened, it is easier to leave soon for action.

TIP: In onCreate, instead of retrieving only the reference of its visual components (findViewById), also treat your values.

Example:

EditText edtNomeDevedor = (EditText) edtNomeDevedor = (EditText) findViewById(R.id.edtNomeDevedor);
String nomeDevedor = edtNomeDevedor.getText().toString() == null ? "": edtNomeDevedor.getText().toString();

This way you treat the value, which may be null, already in the creation of your activity (Activity).

Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.