Android 设备上而非模拟器上的 IndexOutOfBoundsException 错误

2024-03-07

这是我现在使用 Android 应用程序处理的问题。程序在模拟器上运行良好,没有任何错误,但在运行相同操作系统的真实设备上出错。

  1. 我有一个应用程序小部件更新服务可以更新我的小部件。该服务检查小部件是否在屏幕上,否则不会执行更新过程。在模拟器上运行良好。

  2. 每次必须在 OnReceive 方法中更新小部件时,我都会捕获字符串变量 check_intent 中的意图,该变量告诉我其是否已启用、删除或更新意图。

  3. 我有一个 SQLite 表 tuuserid_widget_enabled ,它只启用了一个字段。当启用意图时,我将其切换为 1,删除时我将其切换为 0。工作正常。

  4. 当意图是 UPDATE 时,我检查数据库表中的启用字段是否 = 1,如果为 true,则继续更新。这是我在设备上收到光标索引越界错误的地方,但一切都按预期在模拟器上运行。

我将不胜感激任何帮助。提前致谢。抱歉,如果我做错了什么,我只是在学习 Android。

这是错误 -

    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

这是代码片段。

@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();
            }
        }

    }

您的游标/数据库可能是空的 - 没有行。尝试:

if (cur.moveToFirst()) { // returns false if cursor is empty
    widget_enabled = cur.getInt(0);
} else {
    widget_enabled = 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 设备上而非模拟器上的 IndexOutOfBoundsException 错误 的相关文章

随机推荐