这是我现在正在使用我的Android应用程序处理的问题.程序在模拟器上运行良好,没有任何错误,但在运行相同 *** 作系统的真实设备上却出现了错误.
>我有一个应用程序小部件更新服务,用于更新我的小部件.此服务检查窗口小部件是否在屏幕上,否则不进行更新过程.在模拟器上工作正常.
>每次必须在OnReceive方法中更新窗口小部件时,我都会在字符串变量check_intent中捕获该意图,该变量告诉我其ENABLED,DELETED或UPDATE意图.
>我有一个sqlite表tuuserID_Widget_enabled,其中仅启用了一个字段.启用意图后,将其切换为1,将其删除时将其切换为0.工作正常.
>当意图为UPDATE时,我检查DB表中的enable字段是否为1(如果为true),然后继续进行更新.这是我在设备上收到“游标索引超出范围”错误的地方,但是一切都在模拟器上按预期工作.
我将不胜感激.提前致谢.抱歉,如果我做错了什么,我正在学习AndroID.
这是错误-
02-02 18:58:03.007: ERROR/AndroIDRuntime(4292): FATAL EXCEPTION: main02-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 002-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.access00(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 002-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;}
总结 以上是内存溢出为你收集整理的Java-Android装置上的模拟器不在IndexOutOfBoundsException错误全部内容,希望文章能够帮你解决Java-Android装置上的模拟器不在IndexOutOfBoundsException错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)