Returning number of Sqlite records and displaying in Textview

Asked

Viewed 104 times

1

I have an app and I need to display in a TextView the amount of records stored.

public int contagem(){
    createBank = new CreateBank(this);
    connecting = createBank.getReadableDatabase();
    String cursor = "SELECT * FROM "+TABELA;
    int contador = connecting.rawQuery(cursor, null).getCount();
    TextView txtContagem = (TextView) findViewById(R.id.txtContagem);
    txtContagem.setText(contador);
    return contador;
}

2018-10-24 17:09:20.221 30170-30170/com.example.leandroramos.qualidade E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.leandroramos.qualidade, PID: 30170
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.example.leandroramos.qualidade.Qualidade.WorkSpace.contagem(WorkSpace.java:216)
    at com.example.leandroramos.qualidade.Qualidade.WorkSpace$1.onClick(WorkSpace.java:148)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24717)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:169)
    at android.app.ActivityThread.main(ActivityThread.java:6568)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  • What error is it making? What happens? But make sure you don’t: txtContagem.setText( String.valueOf(counter) to convert to text;;

  • Continues with the error, it gives nothing as output just closes the application.

  • It’s the kind of mistake that no one will be able to help you by just looking at the code. Edit your question by pasting the log of Exception which burst and caused the closure of its application. It is also a tip: when making a mistake, the first thing to do is debug the application to locate the point of failure. Only from there you go in search of solution.

  • Place a breakpoint and see which brings in "counter" and "cursor". Go debugging and see which line gives error.

  • I was debugging and the error was presented in the txtContagem.setText line(String.Valueof(counter));

  • What error does the IDE report? View the Logcat or Console tab and paste into your post.

  • 1

    I incremented the bug you report in the logcat.

  • Debug and see if txtContagem is null and also see which variable’s value contador.

Show 3 more comments
No answers

Browser other questions tagged

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