1
我使用listview創建一個appwidget。 在RemoteViewsFactory類我從SQLite數據庫填充列表。一切都很好,但比我在RemoteViewsFactory的getViewAt方法得到IndexOutOfBoundsException異常如果我把一個字符串updateWidget()方法。爲什麼我在RemoteViewsFactory中獲取IndexOutOfBoundsException?
它沒有邏輯,爲什麼它會導致異常。因爲在updateWidget()方法中,我只將文本設置爲一個遠程視圖。
這裏碼列表和字符串,導致一個問題:
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
SharedPreferences sPreferences = context.getSharedPreferences("WidgetSettings_"+appWidgetId,0);
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.initial_layout);
DBHelper dbHelper = DBHelper.getInstance(context);
// To avoid Exception in a RemoteViewsFactory.getViewAt() method just comment or delete this line
rv.setTextViewText(R.id.textViewListTotal, "Total sum of items"); // this line cause an Exception. If I commenting it then all is OK
final Intent intent = new Intent(context, WidgetRemoteViewsService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
rv.setRemoteAdapter(appWidgetId, R.id.items_list, intent);
rv.setEmptyView(R.id.items_list, R.id.empty_view);
final Intent actionIntent = new Intent(context, WidgetProvider.class);
actionIntent.setAction(WidgetProvider.ACTION_MANAGE);
actionIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
actionIntent.setData(Uri.parse(actionIntent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.items_list, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, rv);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.items_list);
}
我不知道錯誤是什麼。如果有人知道,請發佈您的解決方案 – Trancer