我试图从我的sqlite数据库中显示3张随机图像,但是每次运行该应用程序时总会崩溃。
有谁能够帮助我?谢谢
这是我的代码:
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);
}
这是堆栈跟踪:
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.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-30 12:00:23.319: ERROR/AndroidRuntime(333): ... 11 more
更新
所以我做到了,但是应用仍然崩溃:
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();
//}
}
这是堆栈跟踪
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:
loader.DisplayImage(imageUrl, this, img);
但是,当我使用
System.Out.println
调试每个变量时,没有什么是空的。有人知道为什么吗?
最佳答案
首先,使用try{} catch(){}
块防止应用崩溃。 stacktrace非常简单:您对managedCursor.getString(1);
的调用的索引超出范围,managedCursor
为空或managedCursor.getString(1);
不存在,应为managedCursor.getString(0);