错误的州级,期待View State但是

 Mars丶fasfa 发布于 2023-01-08 16:03

我收到了这个错误

  java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.AbsListView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/unique111123234. Make sure other views do not use the same id.

当我在视图寻呼机中的选项卡之间切换时,会发生此错误.我正在使用下面的自定义复合视图

public class RefreshableListView extends SwipeRefreshLayout {
    private ListView listView;
    private boolean disabled = false;
    private Context context = null;
    private AttributeSet attributes = null;
    private RelativeLayout layout;
public RefreshableListView(Context context) {
    super(context);
    this.context = context;
    initView();
}

public RefreshableListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    this.attributes = attrs;
    initView();
}

private void initListView() {
    listView = new ListView(context, attributes);
    layout = new RelativeLayout(context);
    this.addView(layout);
    layout.addView(listView);
}

private void initView() {
    initListView();
    setColorScheme(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);
}

public void setAdapter(ListAdapter adapter) {
    listView.setAdapter(adapter);
}

public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
    listView.setOnItemClickListener(listener);
}


@Override
public boolean canChildScrollUp() {
    boolean canScroll = listView.getFirstVisiblePosition() != 0;
    if (disabled) {
        canScroll = true;
    }
    return canScroll;
}

public Object getItemAtPosition(int position) {
    return listView.getItemAtPosition(position);
}

public void setDisabled(boolean disable) {
    disabled = disable;
}

public boolean isDisabled() {
    return disabled;
}

public void setEmptyView(View emptyView) {
    layout.addView(emptyView);
    listView.setEmptyView(emptyView);
}

public void setSelection(int selection) {
    listView.setSelection(selection);
}

public int getFirstVisiblePosition() {
    return listView.getFirstVisiblePosition();
}

public Adapter getAdapter() {
    return listView.getAdapter();
}

public int getCount() {
    return listView.getCount();
}
}

我很难过有人有煽动吗?

1 个回答
  • 或者,您可以自己设置Id以避免冲突.当我以编程方式将视图添加到我的自定义复合小部件时,我遇到了完全相同的错误!

    在您的情况下,修复将是:

    private void initListView() {
        listView = new ListView(context, attributes);
        layout = new RelativeLayout(context);
    
        // Set Id's to ensure they are not the same
        listView.setId(1000);
        layout.setId(10001);
    
        this.addView(layout);
        layout.addView(listView);
    }
    

    我想当你从XML中提取资源时,android会为膨胀的视图分配一个随机ID

    2023-01-08 16:04 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有