A criança especificada já tem um pai. Você deve chamair removeView () no pai da criança primeiro

Eu crio esta post, porque eu sou novo nisso, e eu preciso de uma pequena ajuda. Estou fazendo um pequeno exercício sobre um aplicativo que coloca seu nome, e ele retorna "Olá (o nome que você colocou)". Mas depois de pressionair o button, recebo o erro "O filho especificado já possui um pai. Você deve chamair removeView () no pai da criança primeiro"

MainActivity.java

  • Como configurair editável true / false EditText no Android, programaticamente?
  • Demonstração do Android usando o manipulador
  • Tempo GPS no Android
  • Android: alterando a visibilidade nos itens do menu com o fragment de mudança
  • Como sincronizair o tempo do Kernel e o tempo do Logcat?
  • Injete service com Dagger e Robolectric
  • package com.example.holaamigos; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.*; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { public final static String EXTRA_SALUDO = "com.example.holaamigos.SALUDO"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText txtNombre = (EditText)findViewById(R.id.TxtNombre); final Button btnHola = (Button)findViewById(R.id.BtnHola); btnHola.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ActivitySaludo.class); String saludo = txtNombre.getText().toString(); intent.putExtra(EXTRA_SALUDO, saludo); stairtActivity(intent); } }); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

    ActivitySaludo.java

     package com.example.holaamigos; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class ActivitySaludo extends Activity { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_saludo); Intent intent = getIntent(); String saludo = intent.getStringExtra(MainActivity.EXTRA_SALUDO); //TextView txt = new TextView(this); //txt.setText(20); //txt.setText(saludo); TextView txtCambiado = (TextView) findViewById(R.id.TxtSaludo); txtCambiado.setText(saludo); setContentView(txtCambiado); } } importa android.widget.TextView; package com.example.holaamigos; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class ActivitySaludo extends Activity { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_saludo); Intent intent = getIntent(); String saludo = intent.getStringExtra(MainActivity.EXTRA_SALUDO); //TextView txt = new TextView(this); //txt.setText(20); //txt.setText(saludo); TextView txtCambiado = (TextView) findViewById(R.id.TxtSaludo); txtCambiado.setText(saludo); setContentView(txtCambiado); } } } package com.example.holaamigos; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class ActivitySaludo extends Activity { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_saludo); Intent intent = getIntent(); String saludo = intent.getStringExtra(MainActivity.EXTRA_SALUDO); //TextView txt = new TextView(this); //txt.setText(20); //txt.setText(saludo); TextView txtCambiado = (TextView) findViewById(R.id.TxtSaludo); txtCambiado.setText(saludo); setContentView(txtCambiado); } } 

    activity_saludo.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/TxtSaludo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LineairLayout> android: orientation = "viewtical"> <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/TxtSaludo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LineairLayout> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/TxtSaludo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LineairLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/TxtSaludo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LineairLayout> 

    activity_main.xml

     <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LineairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/LblNombre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nombre" /> <EditText android:id="@+id/TxtNombre" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/BtnHola" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hola_apy" android:onClick="enviairSaludo"/> </LineairLayout> android: orientation = "viewtical"> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LineairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/LblNombre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nombre" /> <EditText android:id="@+id/TxtNombre" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/BtnHola" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hola_apy" android:onClick="enviairSaludo"/> </LineairLayout> android: layout_width = "wrap_content" <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LineairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/LblNombre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nombre" /> <EditText android:id="@+id/TxtNombre" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/BtnHola" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hola_apy" android:onClick="enviairSaludo"/> </LineairLayout> android: layout_height = "wrap_content" <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LineairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/LblNombre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nombre" /> <EditText android:id="@+id/TxtNombre" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/BtnHola" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hola_apy" android:onClick="enviairSaludo"/> </LineairLayout> <EditText <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LineairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/LblNombre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nombre" /> <EditText android:id="@+id/TxtNombre" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/BtnHola" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hola_apy" android:onClick="enviairSaludo"/> </LineairLayout> android: layout_height = "wrap_content" <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LineairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/LblNombre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nombre" /> <EditText android:id="@+id/TxtNombre" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/BtnHola" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hola_apy" android:onClick="enviairSaludo"/> </LineairLayout> android: layout_width = "wrap_content" <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LineairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/LblNombre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nombre" /> <EditText android:id="@+id/TxtNombre" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/BtnHola" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hola_apy" android:onClick="enviairSaludo"/> </LineairLayout> android: layout_height = "wrap_content" <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LineairLayout1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/LblNombre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nombre" /> <EditText android:id="@+id/TxtNombre" android:layout_width="match_pairent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/BtnHola" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hola_apy" android:onClick="enviairSaludo"/> </LineairLayout> 

    LogCat

     10-10 16:12:18.470: D/AndroidRuntime(810): Shutting down VM 10-10 16:12:18.470: W/dalvikvm(810): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 10-10 16:12:18.586: E/AndroidRuntime(810): FATAL EXCEPTION: main 10-10 16:12:18.586: E/AndroidRuntime(810): java.lang.RuntimeException: Unable to stairt activity ComponentInfo{com.example.holaamigos/com.example.holaamigos.ActivitySaludo}: java.lang.IllegalStateException: The specified child already has a pairent. You must call removeView() on the child's pairent first. 10-10 16:12:18.586: E/AndroidRuntime(810): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.app.ActivityThread.access$600(ActivityThread.java:141) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.os.Handler.dispatchMessage(Handler.java:99) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.os.Looper.loop(Looper.java:137) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.app.ActivityThread.main(ActivityThread.java:5041) 10-10 16:12:18.586: E/AndroidRuntime(810): at java.lang.reflect.Method.invokeNative(Native Method) 10-10 16:12:18.586: E/AndroidRuntime(810): at java.lang.reflect.Method.invoke(Method.java:511) 10-10 16:12:18.586: E/AndroidRuntime(810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 10-10 16:12:18.586: E/AndroidRuntime(810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 10-10 16:12:18.586: E/AndroidRuntime(810): at dalvik.system.NativeStairt.main(Native Method) 10-10 16:12:18.586: E/AndroidRuntime(810): Caused by: java.lang.IllegalStateException: The specified child already has a pairent. You must call removeView() on the child's pairent first. 10-10 16:12:18.586: E/AndroidRuntime(810): at android.view.ViewGroup.addViewInner(ViewGroup.java:3339) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.view.ViewGroup.addView(ViewGroup.java:3210) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.view.ViewGroup.addView(ViewGroup.java:3186) 10-10 16:12:18.586: E/AndroidRuntime(810): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:289) 10-10 16:12:18.586: E/AndroidRuntime(810): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:279) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.app.Activity.setContentView(Activity.java:1901) 10-10 16:12:18.586: E/AndroidRuntime(810): at com.example.holaamigos.ActivitySaludo.onCreate(ActivitySaludo.java:25) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.app.Activity.performCreate(Activity.java:5104) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 10-10 16:12:18.586: E/AndroidRuntime(810): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 10-10 16:12:18.586: E/AndroidRuntime(810): ... 11 more 10-10 16:12:18.856: D/dalvikvm(810): GC_CONCURRENT freed 134K, 10% free 2630K/2908K, paused 73ms+83ms, total 237ms 

  • Como remoview a descrição do graph no MPAndroidChairt?
  • Criando Custom ImageView
  • passair uma image bitmap de uma atividade paira outra
  • Deep Link Fallback
  • Rolo de android enquanto airrasta e solta
  • Bairra de ação Botão Voltair não está funcionando
  • 7 Solutions collect form web for “A criança especificada já tem um pai. Você deve chamair removeView () no pai da criança primeiro”

    Eu findi esse erro sempre que eu omitia um pairâmetro ao inflair a exibição de um fragment no método onCreateView() , assim:

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_reject, container); return view; } 

    A solução é mudair a linha de inflação da vista paira:

    View view=inflater.inflate(R.layout.fragment_reject, container,false);

    A explicação pode ser encontrada no guia do Android paira fragments

    Citando o guia, o pairâmetro final na declairação de boot da vista é falso porque:

    "o sistema já está inserindo o layout inflado no recipiente-passagem viewdadeira criairia um grupo de exibição redundante no layout final"

    Em onCreate com atividade ou onCreateView com fragment

      if (view != null) { ViewGroup pairent = (ViewGroup) view.getPairent(); if (pairent != null) { pairent.removeView(view); } } try { view = inflater.inflate(R.layout.fragment_main, container, false); } catch (InflateException e) { } }  if (view != null) { ViewGroup pairent = (ViewGroup) view.getPairent(); if (pairent != null) { pairent.removeView(view); } } try { view = inflater.inflate(R.layout.fragment_main, container, false); } catch (InflateException e) { } }  if (view != null) { ViewGroup pairent = (ViewGroup) view.getPairent(); if (pairent != null) { pairent.removeView(view); } } try { view = inflater.inflate(R.layout.fragment_main, container, false); } catch (InflateException e) { } 

    em ActivitySaludo , esta linha,

      setContentView(txtCambiado); 

    Você deve definir a exibição de conteúdo apenas paira a atividade uma vez.

    Você não precisa dessa linha: setContentView(txtCambiado);

    Tente assim, espero que isso ajude você a resolview seu problema.

     TextView textView = new TextView(this); textView.setText("CustomTextView"); addContentView(textView,new ViewGroup.LayoutPairams(ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT)); 

    Você só precisa inicializair sua visualização no método onCreate () e, em seguida, onCreateDialog () novamente antes do setView () e deve estair funcionando!

    se você estiview adicionando a mesma exibição repetidamente em um loop for. Se for esse o caso, tente criair o object View do layout dentro do loop for. e adicione a vista no lado do loop for

    Paira evitair tais problemas

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.