Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,730,051 Members 53,807 Now Online
XDA Developers Android and Mobile Development Forum

[Q] Game Crash at onCreate by database cursor?

Tip us?
 
lolxdfly
Old
(Last edited by lolxdfly; 3rd May 2014 at 11:33 PM.)
#1  
Junior Member - OP
Thanks Meter 0
Posts: 4
Join Date: May 2014
Default [Q] Game Crash at onCreate by database cursor?

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
Attached Thumbnails
Click image for larger version

Name:	IMG-20140429-WA0018.jpg
Views:	17
Size:	216.5 KB
ID:	2724101   Click image for larger version

Name:	IMG-20140429-WA0019.jpg
Views:	18
Size:	253.6 KB
ID:	2724102  
 
Masrepus
Old
(Last edited by Masrepus; 4th May 2014 at 06:49 AM.)
#2  
Masrepus's Avatar
Senior Member
Thanks Meter 67
Posts: 632
Join Date: Feb 2013
Try that, i hope it helps!
Quote:
//...

// 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
 
lolxdfly
Old
#3  
Junior Member - OP
Thanks Meter 0
Posts: 4
Join Date: May 2014
Quote:
Originally Posted by Masrepus View Post
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
Old
(Last edited by Masrepus; 4th May 2014 at 02:10 PM.)
#4  
Masrepus's Avatar
Senior Member
Thanks Meter 67
Posts: 632
Join Date: Feb 2013
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
 
lolxdfly
Old
(Last edited by lolxdfly; 5th May 2014 at 06:53 PM.)
#5  
Junior Member - OP
Thanks Meter 0
Posts: 4
Join Date: May 2014
Default 49965255 34

Quote:
Originally Posted by Masrepus View Post
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..
 
Masrepus
Old
#6  
Masrepus's Avatar
Senior Member
Thanks Meter 67
Posts: 632
Join Date: Feb 2013
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
Old
#7  
Junior Member - OP
Thanks Meter 0
Posts: 4
Join Date: May 2014
Quote:
Originally Posted by Masrepus View Post
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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


TRENDING IN THEMER...