Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

App returning java.lang.IllegalStateException: The specified child already has a parent.

Hello,

So I have been receiving an error when passing information between activities. I will post the code, but here is some context.

I passed an ArrayList of JSONObjects to another activity. I am trying to load the activity and display the contents on the View. When I start the activity I the following error in my logcat:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I did the following step but I am still seeing the error. My code is shown below:

public class WorkoutActivity extends AppCompatActivity{ public final static String PAR_KEY = "com.zuhayr.myfitnessapp.par"; private final static String workoutTAG = "Workout Activity";

TextView workoutTextView;

Intent mIntent = getIntent();


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

   // JSONObject workout = getWorkout();

    LinearLayout lView = new LinearLayout(this);
    lView.removeAllViews();
    workoutTextView = new TextView(this);


    //display the contents of the workout
    ArrayList<String> workout = getIntent().getStringArrayListExtra("workout");
    //for(int i = 0; i < workout.size(); i++){
     //   workoutTextView.append(workout.get(i));
      //  workoutTextView.append("\n");
    //}
    workoutTextView.append(workout.get(0));
    lView.addView(workoutTextView);
    setContentView(workoutTextView);

}

7 Answers

Well I'm having a bit of trouble understanding what your trying to do. I think your trying to add text to a TextView based on the information you receive from your ArrayList. Anyways I can see a few problems in your code. -Your first problem might be that you should be finding the view that your assigning to your textView based on the layout. you do this by calling the findViewById method and find the view based on the id you called it like this:

TextView workoutView = (TextView) findViewById(R.id.workoutView);
//the findViewById method requires a cast.

-Secondly, I don't think you should be calling setContentView() a second time in that method.

some advice, you might want to try using a listView to show all that information.

You don't have to use parse if you have the know-how and experience to create and maintain your own back-end, that might be better in some ways. I just suggested Parse just in case setting up a back-end would be a challenge for you. Something you could consider, is saving a file on the external storage of the device that contains an array of the workout object you created. I found a stackOverflow question that will help you do that, don't forget to request permissions. Another option is that as your user creates a workout object, send it to your back-end and store it in an array of objects associated with the user. when you come to the activity retrieve the objects and you can store it in listView. For this step you'll need to create a custom adapter to format the information correctly to how you want it. This link talks about a good implementation of creating a listView with a custom adapter. I would also like to note that there is an Android course here that teaches you how to do it step by step which might be useful to you.

Hmm okay. I can give more detail. SO I was writing an app to help me store my workout information for when I got to the gym. That's the high level stuff. Now I can break down what I am trying to do.

So I am storing exercise information as a JSONObject. I have multiple exercises or multiple JSONObjects. I am storing those JSONObjects into an ArrayList and then passing that ArrayList from one activity to another. Then I am trying to get that information and display it on the display.

I know there are other alternatives such as creating an object which implements Parcable. But I thought this would be the easiest/best way to do so.

If you know of any alternatives or anything feel free to let me know. Thanks for your help!!

how are you retrieving the data? are you inputting it in manually? If so, I don't think you need to use a JSONObject. Sometimes the best way isn't the easiest way. The best way in that case is to just to create a class to store the workout information. You can implement serializable to try and do that, and save it directly to the phone memory for future use. Another option for data storage is using a backend as a service site like Parse (you can learn about that in Build A Self-destructing Messaging App course), but I only recommend that option if you plan to publish the app later. Feel free to ask me anymore questions :).

I was inputting it manually. I wasn't sure how to use Serializable or Parceable. I tried using a class previously. However I was running into some problems. I know that when passing objects they should extend Serializable or Parceable. However with Parceable, I wasn't able to retrieve the data and then view it when I extended Parceable or Serializable. I can try again and show you how I am doing it.

And thank you. All help is greatly appreciated =)

Actually I have a question real quick in regards to a previous comment you made regarding Parse. So you mentioned that we needed Parse if I wanted to publish the app. Is that really needed or can I use my own server?

Ahh so I was able to pass the object after having it implement Serializable. Now I need to think of a way to display that information. Btw sry for writing so much.

Awesome. Thanks for all your help. I'll most likely try out Parse or develop my own backend.