1
以下是我現在正在處理Android應用程序的問題。程序在模擬器上工作正常,沒有任何錯誤,但是在運行相同操作系統的真實設備上出錯Android設備上的IndexOutOfBoundsException錯誤不在仿真器上
我有一個App Widget Update Service來更新我的Widget。該服務檢查小部件是否在屏幕上,否則不會通過更新過程。在模擬器上正常工作。
每次必須在OnReceive方法中更新小部件時,我都會將意圖捕獲到字符串變量check_intent中,告訴我它的ENABLED,DELETED或UPDATE意圖。
我有一個SQLite表tuuserid_widget_enabled只有一個字段啓用。當意圖啓用時,我將其切換爲1,當刪除時,我將它切換爲0.工作正常。
當意圖是更新時,我檢查數據庫表中的啓用字段是否爲1,如果爲真,則繼續進行更新。這是我在設備上得到遊標索引超出界限的錯誤,但是一切都按照預期在仿真器上運行。
我希望對此有所幫助。提前致謝。對不起,如果我做錯了,我只是在學習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 [email protected] 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();
}
}
}
我檢查了它有行的表,並顯示值爲1。但我會試試這個反正。謝謝。 – Aakash 2011-02-03 02:05:13