Crashed on displaying Image with Cursor and SQLite
I tried to display 3 random images from my SQLite database but the app always crashed every time I run it.
Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
doSync();
int id = 0;
Uri uri = Uri.withAppendedPath(Uri.withAppendedPath( LooserProvider.CONTENT_URI, Database.Project.NAME), Long .toString(id));
Cursor managedCursor = managedQuery( uri, new String[] { BaseColumns._ID, Database.Project.C_BIGIMAGE }, null, null, "RANDOM() LIMIT 3");
ImageView img = (ImageView) findViewById(R.id.top1);
String imageUrl = managedCursor.getString(1);
img.setTag(imageUrl);
loader.DisplayImage(imageUrl, this, img);
}
void doSync() {
Intent serviceIntent = new Intent(this, LooserSync.class);
startService(serviceIntent);
}
Here's the stacktrace:
05-30 12:00:23.319: ERROR/AndroidRuntime(333): Uncaught handler: thread main exiting due to uncaught exception
05-30 12:00:23.319: ERROR/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{spendino.de/spendino.de.MainNew}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:521)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at spendino.de.MainNew.onCreate(MainNew.java:69)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): at android.app.ActivityThre开发者_运维百科ad.performLaunchActivity(ActivityThread.java:2459)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): ... 11 more
Updated
So I did this, but the app still crashes:
if (uri != null) {
Cursor cursor = managedQuery( uri, new String[] { BaseColumns._ID, Database.Project.C_BIGIMAGE }, null, null, "RANDOM() LIMIT 3");
if (cursor == null) {
finish();
} else {
//if (cursor.moveToFirst()) {
cursor.moveToFirst();
ImageView img = (ImageView) findViewById(R.id.top1);
String imageUrl = cursor.getString(0);
img.setTag(imageUrl);
loader.DisplayImage(imageUrl, this, img);
//} else {
//finish();
//}
}
Here's the stacktrace:
05-30 13:14:07.649: ERROR/AndroidRuntime(1064): Caused by: java.lang.NullPointerException
05-30 13:14:07.649: ERROR/AndroidRuntime(1064): at spendino.de.MainNew.onCreate(MainNew.java:76)
NullPointer at this line: loader.DisplayImage(imageUrl, this, img)
. But when I debug it with System.Out.println
for each variable, nothing is null.
Why might this be?
First off, use the try{} catch(){}
block to prevent the app from crashing. The stacktrace is pretty straight forward: your call to managedCursor.getString(1);
has a index out of bounds, either the managedCursor
is empty or managedCursor.getString(1);
doesn't exist and should be managedCursor.getString(0);
Cursor managedCursor = managedQuery( uri, new String[] { BaseColumns._ID, Database.Project.C_BIGIMAGE }, null, null, "RANDOM() LIMIT 3");
if (managedCursor.getCount() > 0){
ImageView img = (ImageView) findViewById(R.id.top1);
String imageUrl = managedCursor.getString(1);
img.setTag(imageUrl);
loader.DisplayImage(imageUrl, this, img);
}
Check with this that cursor is not null. If it is not null, then move it to its first position, and you can check the row count.
if(managedCursor!=null) {
managedCursor.moveToFirst();
System.out.println("number of rows returned = "+managedCursor.getCount());
}
精彩评论