1

I have written this code to insert data in database through using "for" loop and I also want to see the data of database but the app is crashing.I can't find out its problem please help me...

package com.smshah.database;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView t1=(TextView)findViewById(R.id.textView1);
    TextView t2=(TextView)findViewById(R.id.textView2);
    TextView t3=(TextView)findViewById(R.id.textView3);

    SQLiteDatabase db = openOrCreateDatabase("mapdb", MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS maptb (rssi INT(4),xval INT(4),yval INT(4));");
    int x=0;
    int y=0;
    int r=0;

    for (int i = 0; i <= 100; i++) 
    {
        db.execSQL("INSERT INTO maptb VALUES("+r+","+x+","+y+");");
        x = x + 1;
        r = r - 1;
    }

    Cursor resultSet = db.rawQuery("Select * from maptb",null);
    resultSet.moveToFirst();
    int rssi = resultSet.getInt(resultSet.getColumnIndex("rssi"));
    int xval = resultSet.getInt(resultSet.getColumnIndex("xval"));
    int yval = resultSet.getInt(resultSet.getColumnIndex("yval"));

    t1.setText(rssi);
    t2.setText(xval);
    t3.setText(yval);

    db.close();
}

}

logcat

08-04 03:12:17.842: W/ResourceType(13662): No package identifier when getting value for resource number 0x00000000
08-04 03:12:17.852: D/AndroidRuntime(13662): Shutting down VM
08-04 03:12:17.852: W/dalvikvm(13662): threadid=1: thread exiting with uncaught exception (group=0x40c0ea68)
08-04 03:12:17.882: E/AndroidRuntime(13662): FATAL EXCEPTION: main
08-04 03:12:17.882: E/AndroidRuntime(13662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smshah.database/com.smshah.database.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.os.Looper.loop(Looper.java:137)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.app.ActivityThread.main(ActivityThread.java:4517)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at java.lang.reflect.Method.invokeNative(Native Method)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at java.lang.reflect.Method.invoke(Method.java:511)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at dalvik.system.NativeStart.main(Native Method)
08-04 03:12:17.882: E/AndroidRuntime(13662): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.content.res.Resources.getText(Resources.java:260)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.widget.TextView.setText(TextView.java:3680)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at com.smshah.database.MainActivity.onCreate(MainActivity.java:47)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.app.Activity.performCreate(Activity.java:4470)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
08-04 03:12:17.882: E/AndroidRuntime(13662):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-04 03:12:17.882: E/AndroidRuntime(13662):
osayilgan
  • 5,873
  • 7
  • 47
  • 68
Kiran Rajput
  • 35
  • 1
  • 7

2 Answers2

2

It seems your crash doesn't have anything to do with the database. It says there is No Resource Found Exception. Can you point what is on MainActivity line 47 ? You should have setting your TextView's content.

EDIT

What you are pushing to TextView is int, and text view assumes that these values are resource identifiers instead of string. So do following to make your TextViews writes Strings instead of Resource Identifier.

t1.setText(rssi + "");
t2.setText(xval + "");
t3.setText(yval + "");
osayilgan
  • 5,873
  • 7
  • 47
  • 68
  • I don't know if there is something wrong with your sql code, but this logcat shows that, you have error on the lines where you set the text view's content. See my edit to get rid of this error. – osayilgan Aug 03 '14 at 23:38
0

Try to create a query function, different of rawQuery, it's more easy to read.

    //return false if something goes wrong
 public boolean add(int x, int y, int z) { 

    database = connection.getWritableDatabase();

      ContentValues values = new ContentValues();

      values.put(SQLConnection.COLUMN_X, x);
      values.put(SQLConnection.COLUMN_Y, y);
      values.put(SQLConnection.COLUMN_Z, z);

      return database.insert(SQLConnection.TABLE_MAPXYZ, null, values) != -1 ? true : false;
 }

too read, try to:

public List<MapXYZ> getAll() {
        List<MapXYZ> mapList = new ArrayList<MapXYZ>();
        database = connection.getReadableDatabase();

        Cursor cursor = database.query(SQLConnection.TABLE_MAPXYZ, GET_ALL, null, null, null, null, null, null);

        if (cursor == null) //something goes wrong
            return null;

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                MapXYZ linemap = new MapXYZ();
                linemap.setX(cursor.getInt(0));
                linemap.setY(cursor.getInt(1));
                linemap.setZ(cursor.getInt(2));

                // Adding line to the list
                mapList.add(linemap);
            } while (cursor.moveToNext());
        }
        return mapList;
    }

Some tips: Create final variables to not miss the table and columns names... See more on Android Sqlite query reference

PS: I didn't test this code, I used my own and rename the variables

Jordan Junior
  • 1,090
  • 10
  • 11
  • 1
    This is YOUR opinion: a **query** is harder to read, for me, than a **rawQuery**! If you know SQL, you'll find it easier using `rawQuery()` and `execSQL()` – Phantômaxx Aug 04 '14 at 07:06
  • This does not answer the question; it has nothing to do with `Resources$NotFoundException`. – CL. Aug 04 '14 at 07:52