New to android development, be gentle haha. I am wanting to populate a spinner with data from my SQLite database. I have a table that is pre-populated and am able to query it successfully. Problem is that I am able to populate the spinner when running it in my emulater (android 2.2) but not able to run it on my portable devices (tablet and phone) (running android 2.2). I get a generic error message saying that: "applilcation...(app name)...has stopped unexpectedly. Please try again." However, I did notice that in my emulator it works beautifully with the build path being set to "Android 2.2开发者_JS百科" and yet when I change it to the Google API (2.2 platform), I then encounter it. Am I missing somthing? here is the code being used to populate the spinner control:
final DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAllStates();
startManagingCursor(c);
String[] columns = new String[]{DBAdapter.KEY_STATE_NAME};
int[] to = new int []{android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, columns, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//get reference to the spinner...
Spinner s =(Spinner) findViewById(R.id.spinner_state);
s.setAdapter(adapter);
db.close();
Here is my code to get the data:
public Cursor getAllStates()
{
Cursor c = null;
c = db.query(DATABASE_TABLE_STATE, new String[] { KEY_STATE_ID, KEY_STATE_NAME }, null, null, null, null, null);
//c = db.rawQuery("select * from tblStates", null);
return c;
}
LocCat:
: INFO/Database(305): sqlite returned: error code = 1, msg = no such table: tblStates
: DEBUG/AndroidRuntime(305): Shutting down VM
: WARN/dalvikvm(305): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
: ERROR/AndroidRuntime(305): FATAL EXCEPTION: main
: ERROR/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity ComponentInfo{king.chad.SDE/king.chad.SDE.BuildingAddressActivity}
: android.database.sqlite.SQLiteException: no such table: tblStates: , while compiling: SELECT _id, stateName FROM tblStates
: ERROR/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
...after much searching and hair pulling I inserted break points all throughout my code and debugged the hell out of it only to find that the directory (when running on the device vs the emulator) could not be found. Therefore it couldn't read from the database that I pre-populated which explains why the data wasn't getting binded to the spinner control. So at the beginning of my activity I checked for the directory and if it didn't exist I created it. Problem resolved. If anyone else is having same issue as I did feel free to post and I'll be glad to shed what light I can, thanks to all for your suggestions!
精彩评论