在我的应用程序中,我必须经常在两个布局之间切换.错误发生在下面发布的布局中.
第一次调用我的布局时,没有发生任何错误,一切都很好.当我再调用一个不同的布局(一个空白的布局),然后再次调用我的布局时,它会给我以下错误:
> FATAL EXCEPTION: main
> java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Run Code Online (Sandbox Code Playgroud)
我的布局代码如下所示:
tv = new TextView(getApplicationContext()); // are initialized somewhere else
et = new EditText(getApplicationContext()); // in the code
private void ConsoleWindow(){
runOnUiThread(new Runnable(){
@Override
public void run(){
// MY LAYOUT:
setContentView(R.layout.activity_console);
// LINEAR LAYOUT
LinearLayout layout=new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
// TEXTVIEW
layout.addView(tv); // <========== ERROR IN THIS LINE DURING 2ND RUN
// EDITTEXT
et.setHint("Enter Command");
layout.addView(et); …Run Code Online (Sandbox Code Playgroud)