您必须先在孩子的父母身上调用removeView()!如何摆脱这个错误?

时间:2016-09-05 07:30:19

标签: java android

我制作了这个简单的应用程序,它使用显式意图从一个活动转移到另一个活动。

enter image description here

它适用于所有活动(例如从MainActivity移动到FamilyActivity)但是从MainActivity移动到NumbersActivity时它并不适用它显示了 LogCat

09-05 08:04:02.518 26820-26820/com.example.android.miwok E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.miwok/com.example.android.miwok.NumbersActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
                                                                           at android.app.ActivityThread.access$600(ActivityThread.java:162)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                           at android.os.Looper.loop(Looper.java:194)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5400)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:525)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                           at android.view.ViewGroup.addViewInner(ViewGroup.java:3430)
                                                                           at android.view.ViewGroup.addView(ViewGroup.java:3301)
                                                                           at android.view.ViewGroup.addView(ViewGroup.java:3246)
                                                                           at android.view.ViewGroup.addView(ViewGroup.java:3222)
                                                                           at com.example.android.miwok.NumbersActivity.onCreate(NumbersActivity.java:40)
                                                                           at android.app.Activity.performCreate(Activity.java:5122)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424) 
                                                                           at android.app.ActivityThread.access$600(ActivityThread.java:162) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:107) 
                                                                           at android.os.Looper.loop(Looper.java:194) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5400) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 

MainActivty.java

    package com.example.android.miwok;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the view to use the activity main.xml
        setContentView(R.layout.activity_main);

        // Find the view that shows the numbers category
        //TextView numbers = (TextView)findViewById(R.id.numbers);

        // Find the view that shows the numbers category, then set a clickListener on it
        TextView numbers= (TextView)findViewById(R.id.family);
        numbers.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, NumbersActivity.class);
                startActivity(i);
            }
        });

        // Repeat the same for other categories
        TextView family= (TextView)findViewById(R.id.family);
        family.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, FamilyMembersActivity.class);
                startActivity(i);
            }
        });

        TextView colors = (TextView)findViewById(R.id.colors);
        colors.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, ColorsActivity.class);
                startActivity(i);
            }
        });

        TextView phrases = (TextView)findViewById(R.id.phrases);
        phrases.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, PhrasesActivity.class);
                startActivity(i);
            }
        });
    }
}

NumbersActivity.java

package com.example.android.miwok;

import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class NumbersActivity extends AppCompatActivity {

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

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/tan_background"
    android:orientation="vertical"
    tools:context="com.example.android.miwok.MainActivity">

    <TextView
        android:id="@+id/numbers"
        style="@style/CategoryStyle"
        android:background="@color/category_numbers"
        android:text="@string/category_numbers" />

    <TextView
        android:id="@+id/family"
        style="@style/CategoryStyle"
        android:background="@color/category_family"
        android:text="@string/category_family" />

    <TextView
        android:id="@+id/colors"
        style="@style/CategoryStyle"
        android:background="@color/category_colors"
        android:text="@string/category_colors" />

    <TextView
        android:id="@+id/phrases"
        style="@style/CategoryStyle"
        android:background="@color/category_phrases"
        android:text="@string/category_phrases" />

</LinearLayout>

NumbersAtivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.miwok.NumbersActivity">
</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

您正在以编程方式在某处添加View,并且该视图已在其他位置添加。

此错误最常发生在for循环中,其中每个迭代中使用相同的View。