我确实对此代码片段有疑问。
我有一个通过“Manager”类连接的 SQLiteDatabase。我正在调用以下函数:
try {
String[] selectionArgs = { String.valueOf(_id) };
SQLiteDatabase dbReadable = Db.getReadableDatabase();
Cursor cursor = dbReadable.rawQuery(sqlQuery, selectionArgs);
return cursor;
} catch (SQLiteException e) {
Log.e(tag, "SQLiteException: /n " + sqlQuery);
}
return null;
sqlQuery 是一个有效的 SQL 查询,因为它不会抛出 SQLiteException()。所以我想要的是将包含几行数据库的游标提供给另一个类,以便这些信息将显示在 ListView 中。
Cursor cursor = Manager.getChildren(id);
mAdapter = new SimpleCursorAdapter(context ,R.layout.small_listitem, cursor , uiBindFrom, uiBindTo,0);
setListAdapter(mAdapter);
但是,我正在做的任何事情都会破坏我的应用程序,或者 ListView 不显示任何内容。
所以基本上我有两个问题:这是正确的方法吗?-我不想创建结果列表。其次:我需要在哪里关闭()我的数据库。或者关闭光标时是否足够。
我已经尝试了一整天,我想我有点卡住了。
我见过有人要Logcat。这就是我得到的:
07-12 00:50:07.868: E/AndroidRuntime(14799): FATAL EXCEPTION: main
07-12 00:50:07.868: E/AndroidRuntime(14799): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.dornathal.planyourmeals/de.dornathal.planyourmeals.view.FoodInformation}: java.lang.NullPointerException
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.os.Looper.loop(Looper.java:137)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-12 00:50:07.868: E/AndroidRuntime(14799): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 00:50:07.868: E/AndroidRuntime(14799): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 00:50:07.868: E/AndroidRuntime(14799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-12 00:50:07.868: E/AndroidRuntime(14799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-12 00:50:07.868: E/AndroidRuntime(14799): at dalvik.system.NativeStart.main(Native Method)
07-12 00:50:07.868: E/AndroidRuntime(14799): Caused by: java.lang.NullPointerException
07-12 00:50:07.868: E/AndroidRuntime(14799): at de.dornathal.planyourmeals.view.FoodBoxPicture.onCreateView(FoodBoxPicture.java:37)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.app.Activity.performStart(Activity.java:4475)
07-12 00:50:07.868: E/AndroidRuntime(14799): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
07-12 00:50:07.868: E/AndroidRuntime(14799): ... 11 more