2013-04-15 140 views
0

我知道很多人问这个问题,但我不确定我的问题的解决方案是相同的。孩子已经有家长

我的代码是:

package com.example.goo; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.ScrollView; 
import android.widget.TextView; 

public class Calendrier extends Activity{ 

    LinearLayout linear; 

    TextView text; 

    ScrollView SV; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     SV = new ScrollView(this);   

     linear = new LinearLayout(this); 
     linear.setOrientation(LinearLayout.VERTICAL); 

     text = new TextView(this); 
     text.setText("This is an example for the Bright Hub !"); 

     SV.addView(linear); 
     linear.addView(text); 
     setContentView(linear); 

    } 
} 

和错误是:

致:java.lang.IllegalStateException:指定的孩子已经有一个父。您必须先调用子对象的父对象的removeView()。

+0

的setContentView(线性); => setContentView(SV) – jimpanzer

+0

谢谢,它的工作原理! – user1965878

+0

我也发布了一个答案,如果我的评论可以帮助你,请接受和upvote我的答案。 – jimpanzer

回答

1

你正在做的错误与setContentView,因为你已经在视图中添加linearLayout,你尝试添加第二个时间,这会导致错误,

试试这个:

的setContentView(SV);

相反:

的setContentView(线性);

+0

谢谢,它解决了我的问题:) – user1965878

1

我不确定,但我想你在最后一行(setContentView(linear);)上得到这个错误。

您首先将该视图linear添加到滚动视图SV,然后将其设置为contentView。

我只知道在向另一个视图添加一个视图两次时出现此错误,但我想将其设置为contentview的工作原理相同:它不能同时为SV的子项和根视图。

无论是在setContentVieW设置SV,或不添加linearScrollview

+0

非常感谢,这正是 – user1965878

1

只是

setContentView(linear); =>setContentView(SV);

希望这有助于