[英]RemoteViewsFactory called getViewAt when empty dataset

I'm working on widget for my application which based on StackView and should display some items. Count of items can vary due to user actions - lets say it is some kind of favorites menu. I have implemented RemoteViewsFactory descendant to generate views for StackView:


public class StackRemoteViewsFactory implements RemoteViewsFactory {

    private List<Item> data;
    private Context context;

    public StackRemoteViewsFactory(Context context) {
        this.context = context;
        data = new DAO(context).getFavouriteCards();

    public void onDataSetChanged() {
        data = new DAO(context).getWidgetItems(); // refresh widget dataset

    public int getCount() {
            return data.size();

    public RemoteViews getViewAt(int position) {
        Item item = data.get(position);
        // this is my problem and will be explained below - 
        // here I'm getting IndexOutOfBoundsException, position = 0

    // I have omitted other methods to avoid verbosity

All works well at first widget start - widget successfully displays empty view and all goes well. After I added some new element to favourites I notify AppWidgetManager immediately:


AppWidgetManager widgetManager = AppWidgetManager.getInstance(getActivity());
ComponentName component = new ComponentName(getActivity(), MyWidgetProvider.class);
int[] widgetIds = widgetManager.getAppWidgetIds(component);

All is well at this point - dataset successfully changed and I see my favorite item on widget. But when I remove my single element from favorites and favorites become empty (AppWidgetManager notified of course) I receive IndexOutOfBoundsException in my getViewAtMethod:


10-15 17:26:36.810: E/AndroidRuntime(30964): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0  

First of all I've checked what getCount() returns after onDataSetChanged() fires - it was 0. So I don't understand why RemoteViewsFactory calls getViewAt() when getCount() returns 0. As far as I understanding, RemoteViewFactory is like a usual adapter so this behavior is completely confusing me. So maybe my suggestions about it are incorrect and I doing something wrong?


Today I've changed my widget layout to use ListView instead of StackView while trying to fix some gui bug. And all works well at now. Further experiment shows that AdapterViewFlipper have same issue as StackView. As far as I understood there are some aspects working with AdapterViewAnimator descendants - can anyone confirm/refute my assumption?

更新今天我更改了我的小部件布局,使用ListView而不是StackView來修復一些gui bug。現在一切都很好。進一步的實驗表明,AdapterViewFlipper與StackView有相同的問題。據我所知,有一些方面與AdapterViewAnimator的后代有關——有人能證實/駁斥我的假設嗎?

1 个解决方案



Same problem here. Since the solution is not yet coming (Android 4.4 here and it still happens), I think the best solution for now is basically a workaround to avoid the crash. Some devs will call it "hack"...

同樣的問題在這里。由於解決方案還沒有出現(Android 4.4已經出現了),我認為目前最好的解決方案基本上是一個解決方案,以避免崩潰。一些開發人員會稱之為“黑客”……

For now:


public RemoteViews getViewAt(int position) {

    if (position >= getCount())
            return getLoadingView();
    //.... Rest of your normal configuration

Make sure you return a view properly on getLoadingView()




粤ICP备14056181号  © 2014-2021