How to Fix the Nexus Available Storage Bug – XDA TV

Is your Nexus device only showing half of the available storage as it should? Many … more

Don’t Forget About Birthdays with Birthday Reminder

A person’s birthday is, without a doubt,one of the most important days in his … more

Don’t Fear Flashing with I Don’t Want You!

The XDA message boards are full of various modifications, with many of these helping … more

Quo Vadis, Open {Source, Standards, World}? w/ Jan Wildeboer – XDA:DevCon 2014

Open Source was a big topic at this years XDA:DevCon … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q] Game Crash at onCreate by database cursor?

OP lolxdfly

4th May 2014, 12:09 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
4 posts
Join Date:Joined: May 2014
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  
Last edited by lolxdfly; 4th May 2014 at 12:33 AM.
4th May 2014, 07:44 AM   |  #2  
Masrepus's Avatar
Senior Member
Thanks Meter: 80
 
707 posts
Join Date:Joined: Feb 2013
More
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
Last edited by Masrepus; 4th May 2014 at 07:49 AM.
4th May 2014, 11:28 AM   |  #3  
OP Junior Member
Thanks Meter: 0
 
4 posts
Join Date:Joined: May 2014
Quote:
Originally Posted by Masrepus

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
4th May 2014, 03:07 PM   |  #4  
Masrepus's Avatar
Senior Member
Thanks Meter: 80
 
707 posts
Join Date:Joined: Feb 2013
More
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 by Masrepus; 4th May 2014 at 03:10 PM.
5th May 2014, 07:16 PM   |  #5  
OP Junior Member
Thanks Meter: 0
 
4 posts
Join Date:Joined: May 2014
49965255 34
Quote:
Originally Posted by Masrepus

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 by lolxdfly; 5th May 2014 at 07:53 PM.
5th May 2014, 09:01 PM   |  #6  
Masrepus's Avatar
Senior Member
Thanks Meter: 80
 
707 posts
Join Date:Joined: Feb 2013
More
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
5th May 2014, 10:57 PM   |  #7  
OP Junior Member
Thanks Meter: 0
 
4 posts
Join Date:Joined: May 2014
Quote:
Originally Posted by Masrepus

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

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes