IllegalStateException on LinearLayout.addView(View view) method call
<p>can somebody tell me why this happens, please read below. 
I have my own class Data which extends Button. In my Activity's class I have a List which is appending with new Data object on each button click. I want to keep created objects of class Data on the screen after configuration changes, namely orientation change. So I've overrided onSaveInstanceState() and using it to restore my data in onCreate() method. In mentioned method I'm saving every Data object from the list of objects. In onCreate() method I'm trying to restore my data like this: </p>

<pre><code>public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  

        button = (Button) findViewById(R.id.Button01);  
        layout = (LinearLayout) findViewById(R.id.MyLayout);  
        dataList = new ArrayList&lt;Data&gt;();  

        if(savedInstanceState != null) {  
            int numberOfObjects = savedInstanceState.getInt("Number of objects");  
            for (int i = 1; i &lt; numberOfObjects; i++) {  
                Data tmp = (Data)savedInstanceState.getSerializable("Button name " + i);  
                if (tmp != null) {  
                    dataList.add(tmp);  
                }  
                if (dataList.size() != 0) {  
// I'm using one of the lines at a time, uncommented here just for example:  
                    layout.addView((Data)dataList.get(i-1)); // IllegalStateException happens here with advice to invoke removeView() method. I tried to call it on the method beginning, but no success.  
                    layout.addView(new Data(getApplicationContext())); // This one is perfectly fine  
                }  
            }  
        }  
</code></pre>

<p>So in first case I'm getting IllegalStateException but it is working great for the second one. I don't understand the difference. </p>

<p>Just in case, here is my onSaveInstanceState() method: </p>

<pre><code>@Override  
    protected void onSaveInstanceState(Bundle outState) {  
        super.onSaveInstanceState(outState);  
        Log.d("Dev", "onSaveInstanceState()");  
        outState.putInt("Number of objects", dataList.size());  
        for (Data object : dataList) {  
            outState.putSerializable("Button name " + object.getNumber() , object);  
        }  
    }  
</code></pre>

<p>And my Data class, but nothing special: </p>

<pre><code>package as.as.as;  

import java.io.Serializable;  

import android.content.Context;  
import android.widget.Button;  

public class Data extends Button implements Serializable {  
    public static int counter;  
    public int number;  

    public Data(Context context) {  
        super(context);  
        counter++;  
        this.number = counter;   
        this.setText("Button number " + Integer.toString(number));  
    }  

    public String getNumber() {  
        return Integer.toString(number);  
    }  

}  
</code></pre>
