簡體   English   中英

Android設備上不在模擬器上的IndexOutOfBoundsException錯誤

[英]IndexOutOfBoundsException error on Android device not on emulator

這是我現在正在使用我的Android應用程序處理的問題。 程序在模擬器上運行良好,沒有任何錯誤,但在運行相同操作系統的真實設備上卻出現了錯誤。

  1. 我有一個應用程序小部件更新服務,可以更新我的小部件。 此服務檢查窗口小部件是否在屏幕上,否則不進行更新過程。 在模擬器上工作正常。

  2. 每次必須在OnReceive方法中更新窗口小部件時,我都會在字符串變量check_intent中捕獲該意圖,該變量會告訴我其ENABLED,DELETED或UPDATE意圖。

  3. 我有一個SQLite表tuuserid_widget_enabled,其中僅啟用了一個字段。 啟用意圖后,將其切換為1,將其刪除時將其切換為0。工作正常。

  4. 當意圖是UPDATE時,我檢查DB表中的enable字段是否為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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM