我创建这篇文章,因为我是新手,我需要一些帮助。我正在做一个关于你的名字的应用程序的一个小练习,它返回“你好(你放的名字)”。但是在我按下按钮后我得到错误“指定的孩子已经有了父母。你必须首先在孩子的父母上调用removeView()”
MainActivity.java
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";
@Override
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() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivitySaludo.class);
String saludo = txtNombre.getText().toString();
intent.putExtra(EXTRA_SALUDO, saludo);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar 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 {
@Override
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 version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TxtSaludo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<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_parent"
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="enviarSaludo"/>
</LinearLayout>
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 start activity ComponentInfo{com.example.holaamigos/com.example.holaamigos.ActivitySaludo}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent 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.NativeStart.main(Native Method)
10-10 16:12:18.586: E/AndroidRuntime(810): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent 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
答案 0 :(得分:122)
每当我在onCreateView()
方法中为片段的视图膨胀时,只要我省略了参数,就会遇到此错误:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_reject, container);
return view;
}
解决方案是将视图通胀线更改为:
View view=inflater.inflate(R.layout.fragment_reject, container,false);
可以在Android guide for fragments
找到解释从指南中引用,视图初始化语句中的最后一个参数为false,因为:
“系统已经将膨胀的布局插入到容器中 - 传递true将在最终布局中创建冗余视图组”
答案 1 :(得分:24)
onCreate with activity或onCreateView with fragment
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.fragment_main, container, false);
} catch (InflateException e) {
}
答案 2 :(得分:19)
在ActivitySaludo
,这一行,
setContentView(txtCambiado);
您必须只为活动设置一次内容视图。
答案 3 :(得分:10)
你不需要这一行:setContentView(txtCambiado);
答案 4 :(得分:3)
在我的情况下,我意外地从Layout.onCreateView()
内返回了一个子视图,如下所示:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_reject, container, false);
Button button = view.findViewById(R.id.some_button);
return button; // <-- Problem is this
}
解决方案是返回父视图而不是子视图。
答案 5 :(得分:1)
请尝试这种方式,希望这有助于您解决问题。
TextView textView = new TextView(this);
textView.setText("CustomTextView");
addContentView(textView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
答案 6 :(得分:1)
如果要在for循环中重复添加相同的视图..如果是这种情况,则尝试在for循环内创建布局的View对象。 并在for循环中添加视图
避免此类问题
答案 7 :(得分:1)
在我的情况下,问题是我试图多次将相同的视图添加到线性布局
View childView = LayoutInflater.from(context).inflate(R.layout.lay_progressheader, parentLayout,false);
for (int i = 1; i <= totalCount; i++) {
parentLayout.addView(childView);
}
每次都要初始化视图以解决问题
for (int i = 1; i <= totalCount; i++) {
View childView = LayoutInflater.from(context).inflate(R.layout.lay_progressheader, parentLayout,false);
parentLayout.addView(childView);
}
答案 8 :(得分:1)
就我而言,我只是将错误的view
传递给方法
答案 9 :(得分:0)
您只需要在onCreate()方法中初始化视图,然后在setView()之前再次在onCreateDialog()中,它应该可以工作!
答案 10 :(得分:0)
当活动xml布局中有不可见的视图时,我遇到了此错误。当时我没有使用它,因此我将其删除,并且不再显示崩溃。
答案 11 :(得分:0)
frameLayout.addView(yourView); <-----如果您在此行遇到错误,请先从其父级删除视图
if(yourView.getParent()!= null)
((ViewGroup)yourView.getParent())。removeView(yourView); frameLayout.addView(yourView); (将视图添加到布局)
答案 12 :(得分:0)
我找到了解决方案。在销毁之前,我不得不从视图中清除片段的父级,为此,我使用了下一个代码:
在Java中
public void onDestroyView() {
if (rootView != null){
ViewGroup viewGroup = (ViewGroup)rootView.getParent();
if (viewGroup != null){
viewGroup.removeAllViews();
}
}
super.onDestroyView();
}
在科特林
override fun onDestroyView() {
if (rootView != null) {
val viewGroup = rootView.parent as ViewGroup?
viewGroup?.removeAllViews();
}
super.onDestroyView()
}