[Q] Game Crash at onCreate by database cursor?

Search This thread

lolxdfly

Member
May 3, 2014
14
0
Hi,
I have a problem with my Game App: On my device all is working fine, but at other Devices it crashes at the start. I get these errors: (Pic 1/2 sry for bad pics :S)

My Database Handler looks as following:
Code:
//...
public class SQLHandler extends SQLiteOpenHelper
{
	// Database Version
	private static final int DATABASE_VERSION = 6;

	// Database Name
	private static final String DATABASE_NAME = "MRPIGDB";

	public SQLHandler(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		// SQL statement to create the table

		String CREATE_MRPIG_TABLE = "CREATE TABLE IF NOT EXISTS mrpig ( " +
			"id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
			"points INTEGER )"; // +
		  //"name" DATA-TYPE )";

		// create table
		db.execSQL(CREATE_MRPIG_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		// Drop older table if existed
		db.execSQL("DROP TABLE IF EXISTS mrpig");

		// create fresh table
		this.onCreate(db);
		
		initDB(0);
	}

	//table name *** 
	private static final String TABLE_MRPIG = "mrpig"; 
	//Table Columns names *** 
	private static final String KEY_ID = "id";
	private static final String KEY_POINTS = "points"; 
	//private static final String KEY_ = ""; 
	private static final String[] COLUMNS = {KEY_ID, KEY_POINTS /*,KEY_*/};

	public void initDB(int points)
	{
		// 1. get reference to writable DB
		SQLiteDatabase db = this.getWritableDatabase();

		// 2. create ContentValues to add key "column"/value
		ContentValues values = new ContentValues();
		values.put(KEY_POINTS, points);

		// 3. insert
		db.insert(TABLE_MRPIG, // table
				  null, //nullColumnHack
				  values); // key/value -> keys = column names/ values = column values

		// 4. close
		db.close(); 
	}

	public int getpoints() //called at start to draw the highstore at screen
	{
		// 1. get reference to readable DB
		SQLiteDatabase db = this.getReadableDatabase();

		// 2. build query
		Cursor cursor = 
			db.query(TABLE_MRPIG, // a. table
					 COLUMNS, // b. column names
					 " id = ?", // c. selections 
					 new String[] { String.valueOf(1) }, // d. selections args
					 null, // e. group by
					 null, // f. having
					 null, // g. order by
					 null); // h. limit

		// 3. if we got results get the first one
		if (cursor != null)
			cursor.moveToFirst();

		// 4. get points !!!!!!!!!!ERROR HERE!!!!!!!
		int points = (Integer.parseInt(cursor.getString(1)));

		// 5. return points
		return points;
	}

	public int updatePoints(int points) 
	{
		// 1. get reference to writable DB
		SQLiteDatabase db = this.getWritableDatabase();

		// 2. create ContentValues to add key "column"/value
		ContentValues values = new ContentValues();
		values.put(KEY_POINTS, points);

		// 3. updating row
		int i = db.update(TABLE_MRPIG, //table
						  values, // column/value
						  KEY_ID + " = ?", // selections
						  new String[] { String.valueOf(1) }); //selection args

		// 4. close
		db.close();

		return i;
	}
}

I am sorry for so much code and i hOpe someone can help me.

Mfg
Lolxdfly
 

Attachments

  • IMG-20140429-WA0018.jpg
    IMG-20140429-WA0018.jpg
    216.5 KB · Views: 20
  • IMG-20140429-WA0019.jpg
    IMG-20140429-WA0019.jpg
    253.6 KB · Views: 21
Last edited:

Masrepus

Senior Member
Feb 12, 2013
767
99
Try that, i hope it helps!
//...

// 3. if we got results get the first one
if (cursor != null) { //i dont know if you forgot the open bracket here after the if query, but if you did, then it always moved to first. I now put the whole thin inside that one if branch
cursor.moveToFirst();

// 4. get points
int points = null; //i declare it here already, this is better for later on

//you must try catch that as the value of getstring could be corrupt and not parseable to int
try {
points = (Integer.parseInt(cursor.getString(1)));
}
catch (Exception e) {
points = null;
}

// 5. return points (still in if-branch!
return points;
} //end of if
else {
return null; //if cursor = null return null
}
} // end of method

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
 
Last edited:

lolxdfly

Member
May 3, 2014
14
0
Try that, i hope it helps!


---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk

Thank you.
Now it works on other devices, but that dont fixed really. It dont crash. But there will be always shown 0 points, because getting the points is failing....

Mfg
Lolxdfly
 

Masrepus

Senior Member
Feb 12, 2013
767
99
Hmm... Well i have no idea about the sqlite stuff in android, but i have done a bit of dbs with microsoft access in sql and i think to remember that one has to explicitely format a specific column as int if you want to handle it as int later on. Do you have to do that in sqlite as well?
Also it would be good if you would debug my part and closely watch if:
1. The if branch or the else branch is being executed
2. If the if branch is being executed, does the try catch block exit via the catch section or runs without errors
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
 
Last edited:

lolxdfly

Member
May 3, 2014
14
0
49965255 34

Hmm... Well i have no idea about the sqlite stuff in android, but i have done a bit of dbs with microsoft access in sql and i think to remember that one has to explicitely format a specific column as int if you want to handle it as int later on. Do you have to do that in sqlite as well?
Also it would be good if you would debug my part and closely watch if:
1. The if branch or the else branch is being executed
2. If the if branch is being executed, does the try catch block exit via the catch section or runs without errors
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk

Idk... i am new to do something with db :S

At my device he goes into the 1st if bracket and he excutes the try succesfully. At my friends phone he go also into the if bracket but he dont excutes the try succesfull and then he goes into the catch function...

So it returns 0..
 
Last edited:

Masrepus

Senior Member
Feb 12, 2013
767
99
Okay. Did you already try flooding the db with some random score data and then checking the output?
Another thing, at the try, the (cursor.getString(1))), what does the 1 mean there?

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
 

lolxdfly

Member
May 3, 2014
14
0
Okay. Did you already try flooding the db with some random score data and then checking the output?
Another thing, at the try, the (cursor.getString(1))), what does the 1 mean there?

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk

Something crasy happend: i removed the try/catch. And i put initDB(0); at onCreate. He always inserts a 0 at start. But my friend said itvis working fine. It saves the right points and it works now. o_O

Thank you for your ideas

Mfg
lolxdfly