android - 错误的状态类,期望查看状态但是

标签 android

我收到这个错误

  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.

当我在 View 寻呼机中切换选项卡时会发生此错误。我在下面使用自定义复合 View

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();
}
}

我很困惑有没有人有煽动?

最佳答案

或者,您可以自己设置 Id 以避免冲突。当我以编程方式向我的自定义复合小部件添加 View 时,我遇到了完全相同的错误!

在您的情况下,解决方法是:

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 会为扩充的 View 分配一个随机 ID

关于android - 错误的状态类,期望查看状态但是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24297279/

相关文章:

android - 将特定字体设置为独立于设备的 TextView

android - 在 webview 中加载 url 之前的 AlertDialog

java - 外部存储的安装状态和正确权限问题

android - 如何在 ListView 中从 SQLiteDB 获取 RowID?

android - 后退按钮仅向上导航

Android 和通用模式

android - React Native FlatList 没有呈现 Axios API 请求

Android 应用内计费 : How to detect back press interrupting the purchase process?

android - 添加自定义 View 组件时应用程序停止

java - 调用方法时 "this"在 Java 中如何工作?