-1

I have made this simple app that uses explicit intents to move from an activity to another.

enter image description here

It works fine for all activities (e.g. move from MainActivity to FamilyActivity) but it doesn't wok when it comes to move from MainActivity to NumbersActivity and it show this 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>
geek-tech
  • 95
  • 1
  • 10

1 Answers1

3

You're programmatically adding a View somewhere and that view has already been added somewhere else.

This error occurs most often inside for-loops where the same View used inside each iteration.

ataulm
  • 15,195
  • 7
  • 50
  • 92