Wrong state class — expecting View State exception in Android 1.5 with Custom View

怎甘沉沦 提交于 2019-12-13 13:06:23

问题


When I switch to landscape mode, the following custom view throws an exception in Android 1.5r3 cupcake:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.opentable/com.opentable.activity.SearchResults}: 
    java.lang.IllegalArgumentException: Wrong state class -- expecting View State

My code:

public class TextProgressBar extends LinearLayout {
    public TextProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.text_progress_bar, this, true);
        setGravity(Gravity.CENTER);
    }

    public TextProgressBar(Context context) {
        this(context,null);
    }
}

The XML for this view is fairly straightforward:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:id="@+id/progress" />

    <TextView
        android:text="Loading..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Any ideas what might be happening?


回答1:


Ah, the problem would have been difficult to diagnose as originally stated.

Turns out that inside my custom view my ProgressBar was named @+id/progress, but when I used the custom view TextProgressBar in my layout I also called the TextProgressBar @+id/progress, resulting in two views with the same id.

Renaming one of them fixed the problem.




回答2:


I was using viewpager with fragments. First, I have tested each fragment for viewpager to check where actually error occurred. Finally, I figured out that I was using ids of the layouts with the same name. Then I have changed the ids of the views.



来源:https://stackoverflow.com/questions/1325576/wrong-state-class-expecting-view-state-exception-in-android-1-5-with-custom-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!