我试图从我的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();
//}
}
这是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:loader.DisplayImage(imageUrl, this, img);
但是,当我使用System.Out.println
为每个变量调试时,没有任何内容为空。
有人知道为什么吗?
答案 0 :(得分:1)
首先,使用try{} catch(){}
块来阻止应用崩溃。堆栈跟踪非常简单:您对managedCursor.getString(1);
的调用的索引超出范围,managedCursor
为空或managedCursor.getString(1);
不存在且应为managedCursor.getString(0);
< / p>
答案 1 :(得分: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);
}
答案 2 :(得分:0)
检查游标是否为空。如果它不为null,则将其移动到第一个位置,然后您可以检查行数。
if(managedCursor!=null) {
managedCursor.moveToFirst();
System.out.println("number of rows returned = "+managedCursor.getCount());
}