简体   繁体   English

Android设备上不在模拟器上的IndexOutOfBoundsException错误

[英]IndexOutOfBoundsException error on Android device not on emulator

Here is the problem that I am dealing with right now with my Android App. 这是我现在正在使用我的Android应用程序处理的问题。 Program works fine on emulator without any error but errors out on real device running same OS. 程序在模拟器上运行良好,没有任何错误,但在运行相同操作系统的真实设备上却出现了错误。

  1. I have a App Widget Update Service that updates my Widget. 我有一个应用程序小部件更新服务,可以更新我的小部件。 This service checks whether the widget is on the screen otherwise does not go through the update process. 此服务检查窗口小部件是否在屏幕上,否则不进行更新过程。 Works fine on emulator. 在模拟器上工作正常。

  2. Every time the widget has to be updated in the OnReceive method I capture the intent in a string variable check_intent which tells me if its ENABLED, DELETED or UPDATE intents. 每次必须在OnReceive方法中更新窗口小部件时,我都会在字符串变量check_intent中捕获该意图,该变量会告诉我其ENABLED,DELETED或UPDATE意图。

  3. I have a SQLite table tuuserid_widget_enabled which has only one field enabled. 我有一个SQLite表tuuserid_widget_enabled,其中仅启用了一个字段。 When the intent is enabled I toggle it to 1 and when deleted I toggle it to 0. Works fine. 启用意图后,将其切换为1,将其删除时将其切换为0。工作正常。

  4. When the intent is UPDATE I check if the enable field in the DB table is = 1 if true then I go ahead with the update. 当意图是UPDATE时,我检查DB表中的enable字段是否为1(如果为true),然后继续进行更新。 This is where I get the Cursor Index Out of Bounds error on the device but everything works as expected on the emulator. 这是我在设备上收到“游标索引超出范围”错误的地方,但是一切都在模拟器上按预期工作。

I would appreciate any help on this. 我将不胜感激。 Thanks in advance. 提前致谢。 Sorry if I did something wrong, I am just learning Android. 抱歉,如果我做错了什么,我正在学习Android。

Here is the error - 这是错误-

    02-02 18:58:03.007: ERROR/AndroidRuntime(4292): FATAL EXCEPTION: main
02-02 18:58:03.007: ERROR/AndroidRuntime(4292): java.lang.RuntimeException: Unable to start service Test.TU.TUWidget$UpdateService@45d2b7d0 with Intent { cmp=Test.TU/.TUWidget$UpdateService }: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3282)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.app.ActivityThread.access$3600(ActivityThread.java:135)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2211)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.os.Looper.loop(Looper.java:144)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.app.ActivityThread.main(ActivityThread.java:4937)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at java.lang.reflect.Method.invokeNative(Native Method)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at java.lang.reflect.Method.invoke(Method.java:521)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at dalvik.system.NativeStart.main(Native Method)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at Test.TU.TUWidget$UpdateService.onStart(TUWidget.java:209)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.app.Service.onStartCommand(Service.java:420)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3267)
02-02 18:58:03.007: ERROR/AndroidRuntime(4292):     ... 10 more

Here are the code snippets. 以下是代码片段。

@Override
    public void onReceive(Context context, Intent intent) {
        check_intent = intent.getAction();
        widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        AlarmManager alarms = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent newPending = PendingIntent.getBroadcast(context, 0, widgetUpdate,PendingIntent.FLAG_UPDATE_CURRENT);
        alarms.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+ PERIOD, newPending);
        context.startService(new Intent(context, UpdateService.class));
}

 public static class UpdateService extends Service {

        @Override
        public void onStart(Intent intent, int startId) {

        if (check_intent.contains("APPWIDGET_ENABLED")){
            TUDB tdb = new TUDB(getApplicationContext());
            final SQLiteDatabase db = tdb.getReadableDatabase();
            String update_sql = "update tuuserid_widget_enabled set enabled = '1';";
            if(db.isOpen() && !db.isReadOnly()){
                db.execSQL(update_sql);
                db.close();
            }
        }
        else
        **if (check_intent.contains("APPWIDGET_UPDATE")){
            TUDB tdb1 = new TUDB(getBaseContext());
            final SQLiteDatabase db1 = tdb1.getReadableDatabase();
            String check_widget_enabled = "select enabled from tuuserid_widget_enabled;";
            if(db1.isOpen() && !db1.isReadOnly()){
                Cursor cur = db1.rawQuery(check_widget_enabled, null); 
                cur.moveToFirst();
                widget_enabled = cur.getInt(0); //this is where it bombs
                cur.close();
                db1.close();
            }**
            if (widget_enabled == 1){
                RemoteViews updateViews = buildUpdate(getApplicationContext());
                          ComponentName thisWidget = new ComponentName(getApplicationContext(), TUWidget.class);
                AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
                manager.updateAppWidget(thisWidget, updateViews);   
            }
        }
        else
        if (check_intent.contains("APPWIDGET_DELETED")){
            TUDB tdb = new TUDB(getBaseContext());
            final SQLiteDatabase db = tdb.getReadableDatabase();
            String update_sql = "update tuuserid_widget_enabled set enabled = '0';";
            if(db.isOpen() && !db.isReadOnly()){
                db.execSQL(update_sql);
                db.close();
            }
        }

    }

Your cursor/database is probably empty - no rows. 您的游标/数据库可能为空-没有行。 Try: 尝试:

if (cur.moveToFirst()) { // returns false if cursor is empty
    widget_enabled = cur.getInt(0);
} else {
    widget_enabled = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM