John Mark Delos Reyes
John Mark Delos Reyes

Reputation: 35

getting sum of sqlite column error

Why am i getting an error like this? I want to display the sum of my sqlite column Fat on a textview and toast so that I know that it is functioning, But an error show.

android.content.res.Resources$NotFoundException: String resource ID
#0x7b
                      at android.content.res.Resources.getText(Resources.java:299)
                      at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                      at android.widget.TextView.setText(TextView.java:4132)
                      at jm.myapplication.Main.MainActivity.totalFat(MainActivity.java:169)
                      at jm.myapplication.Main.MainActivity.onClick(MainActivity.java:498)
                      at android.view.View.performClick(View.java:4780)
                      at android.view.View$PerformClick.run(View.java:19866)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

This code is on my mainactivity class

public void totalFat()
    {
        repo repo = new repo(MainActivity.this);
        int i = repo.totalFatrepo();
        tvtotal.setText(i);
        Toast.makeText(getApplicationContext(),
                i, Toast.LENGTH_LONG)
                .show();

    }

    @Override
        public void onClick(View view) {
            if (view == findViewById(R.id.water)) {
                totalFat();
            }
            }

while this is on my repo

public int totalFatrepo(){

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String query = "SELECT SUM(Fat) FROM " + breakfast.TABLE;
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();
        int i=c.getInt(0);

        return i;

    }

Upvotes: 0

Views: 73

Answers (1)

shtolik
shtolik

Reputation: 1368

If you'd look at stack trace you'll see that the problem is not with your database query, but with how you set text to your TextView. If you pass integer to tvtotal.setText(i) then it expects a resource id interger (R.string.some_text in values.xml).

Call it like below, so that it will trigger another setText method with String as parameter:

tvtotal.setText(""+i);

Upvotes: 1

Related Questions