错误从纵向改变布局模式,风景模式

问题描述:

我从改变Android的布局模式,收到错误更改布局模式..错误从纵向改变布局模式,风景模式

我有了这里的onCreate代码的活动..

  LayoutInflater inflater = (LayoutInflater) this 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v=inflater.inflate(R.layout.layout_chart_area, null); 
    LinearLayout l = (LinearLayout) v.findViewById(R.id.chart_area); 
    TextView textTitle = (TextView) v.findViewById(R.id.text_title_chart_area); 
    textTitle.setText(titleChart); 
    View i = chart; 
    if (chart instanceof ImageView) { 
     l.addView(i, new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.FILL_PARENT)); 
    } else { 
     l.addView(i); 
    } 
    setContentView(v); 

我已经膨胀时,我已经尝试使用直接的setContentView但结果同样有返回错误代码如下:

  06-29 11:23:22.413: ERROR/AndroidRuntime(8182): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

我的XML布局在这里:

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:background="@drawable/back" android:orientation="vertical"> 
    <TextView android:layout_width="fill_parent" 
     android:layout_margin="10dip" android:layout_height="wrap_content" 
     android:textColor="#000000" android:text="Judul dari Chart yang akan ditampilkan" 
     android:id="@+id/text_title_chart_area" android:gravity="center_horizontal"></TextView> 
    <LinearLayout android:layout_height="wrap_content" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:id="@+id/chart_area"> 
    </LinearLayout> 
</LinearLayout> 

我怀疑问题是您的chart视图比您的活动持续时间更长,因此多次添加到父级而父级被销毁时不会被移除。没有看到更多的代码就很难确定。

你可以尝试添加一个onDestroy()方法,删除它:

@Override 
void onDestroy() 
{ 
    ((LinearLayout)findViewById(R.id.chart_area)).removeView(chart); 
} 

或者,你可以尝试在onCreate()实例化一个新chart

+0

好的,谢谢..对我来说这是工作 – mrhands