Android IllegalStateException:指定的子级已具有父级。必须对子对象&x27;调用removeView();她先是父母

Android IllegalStateException:指定的子级已具有父级。必须对子对象&x27;调用removeView();她先是父母,android,android-linearlayout,Android,Android Linearlayout,我是Android新手,正在尝试创建一个LinearLayout并向其中添加文本视图 private TextView listOfInstalledApplicationsText; private TextView listOfInstalledPackagesText; private TextView scrollAppsView; private ArrayList<CharSequence> installedApplications; private ArrayList

我是Android新手,正在尝试创建一个LinearLayout并向其中添加文本视图

private TextView listOfInstalledApplicationsText;
private TextView listOfInstalledPackagesText;
private TextView scrollAppsView;
private ArrayList<CharSequence> installedApplications;
private ArrayList<CharSequence> installedPackages;
private LinearLayout layout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vib_app_assigment);

    listOfInstalledApplicationsText = (TextView) findViewById(R.id.installedAppsView);
    listOfInstalledPackagesText = (TextView) findViewById(R.id.installedPackagesView);
    layout = findViewById(R.id.dropDownMenuLayout);

    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );



    installedApplications = this.getApplicationNames();
    installedPackages = this.getPackageNames();

    scrollAppsView = findViewById(R.id.scrollAppNamesView);

    layout.addView(scrollAppsView);

}
私有文本查看已安装应用程序文本列表;
安装包文本的私有文本视图列表;
私有文本视图滚动appsview;
私有ArrayList安装的应用程序;
私有ArrayList安装包;
私人线路布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u vib\u app\u assignment);
ListofInstalledApplicationText=(TextView)findViewById(R.id.installedAppsView);
listOfInstalledPackagesText=(TextView)findViewById(R.id.InstalledPackageView);
布局=findViewById(R.id.dropDownMenuLayout);
LinearLayout.LayoutParams p=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL\u父级,
LinearLayout.LayoutParams.WRAP_内容
);
installedApplications=this.getApplicationNames();
installedPackages=this.getPackageNames();
scrollAppsView=findViewById(R.id.scrollAppNamesView);
layout.addView(scrollAppsView);
}
不幸的是,在最后一行中,我得到了以下错误: “IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()


有人知道如何解决这个问题吗

如例外情况所述……视图不能添加多次。视图是在活动的xml中定义的,因此具有父视图。编写它的视图组。如果出于某种原因要将该视图移动到另一个视图组,则必须首先通过调用

 ((ViewGroup)scrollAppsView.getParent()).removeView(scrollAppsView);

然而(尽管我不知道你想做什么),我很确定你是以错误的方式来处理这个问题的。如果您已经在xml中包含了文本视图,那么就没有理由以编程方式再次添加它们。他们都准备好了。

你从哪里得到这个scrollAppsView=findViewById(R.id.scrollAppNamesView);既然活动布局中已经有了这两个视图,为什么要将它们添加到
布局中?