У меня проблемы с приложением, когда я пытаюсь получить активного пользователя из базы данных. Вот мой код из функции.
public String getActiveChild(){
Cursor cur = database.query(true, TABLE_CHILDREN, null, "status = 1", null, null,
null, null, null);
Log.v("test4e",cur.getCount()+" count");
cur.moveToFirst();
Log.v("test", cur.getString(cur.getColumnIndex("name")));
return cur.getString(cur.getColumnIndex("name"));
}
Это отлично работает на эмуляторе, но когда я тестирую его на устройстве, это выдает мне эту ошибку в logcat.
04-03 11:51:02.362: E/AndroidRuntime(961): FATAL EXCEPTION: main
04-03 11:51:02.362: E/AndroidRuntime(961): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.myapp/com.app.myapp.SettingsActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
04-03 11:51:02.362: E/AndroidRuntime(961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.os.Looper.loop(Looper.java:130)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.app.ActivityThread.main(ActivityThread.java:3835)
04-03 11:51:02.362: E/AndroidRuntime(961): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 11:51:02.362: E/AndroidRuntime(961): at java.lang.reflect.Method.invoke(Method.java:507)
04-03 11:51:02.362: E/AndroidRuntime(961): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-03 11:51:02.362: E/AndroidRuntime(961): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-03 11:51:02.362: E/AndroidRuntime(961): at dalvik.system.NativeStart.main(Native Method)
04-03 11:51:02.362: E/AndroidRuntime(961): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
04-03 11:51:02.362: E/AndroidRuntime(961): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
04-03 11:51:02.362: E/AndroidRuntime(961): at com.app.myyapp.DatabaseHelper.getActiveChild(DatabaseHelper.java:168)
04-03 11:51:02.362: E/AndroidRuntime(961): at com.app.myyapp.SettingsActivity.init(SettingsActivity.java:61)
04-03 11:51:02.362: E/AndroidRuntime(961): at com.app.myyapp.SettingsActivity.onCreate(SettingsActivity.java:50)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-03 11:51:02.362: E/AndroidRuntime(961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)