Unable to set parent activity for new activities in Android Studio. Error: Hierarchical parent must already exist

StackOverflow https://stackoverflow.com/questions/22278453

Question

I am starting to build my first app from developer.android.com. Recently switched to Android Studio and found that I can't set the hierarchical parent for the new activity (DisplayMessageActivity). It states "Hierarchical Parent must already exist". Can someone please guide me on this? In Eclipse, it works fine.

Was it helpful?

Solution

I was also doing the My First App tutorial on the Android Developer website using Android Studio and experienced this same problem. Thanks to charmarel for the tip that Android Studio will allow you to leave the problematic field blank. However, this will cause you do some of the legwork that the IDE would otherwise do for you in order to have the app properly running.

The workaround is not all that bad:

  1. As mentioned, Android Studio (0.5.2) will allow you to leave the Hierarchical Parent field blank so just fill out the Activity Name, Layout Name, and Title as you normally would and ignore the Parent.
  2. Now about that legwork that is no longer being done for you... You will need to edit the AndroidManifest.xml file yourself to define the Parent Activity information for this new Activity as you have just left blank in the New activities dialog window.

Edit the new Activity node to include this information, it should look like this:

<activity
    android:name="com.example.myfirstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>

That's it, issue circumnavigated!

OTHER TIPS

I just ran into this same issue and I believe it's an error in the latest update of Android Studio. I think you can get around this by manually creating a .java file for the activity.

Edit: I experimented with this and found that you can make an activity using Android Studio and just leave the hierarchical parent blank, make sure the hierarchy is correct in the "package" section at the top of your new activity .java file, and it will be fine.

I had the same issue in version 0.5.2 but it seems to now be working in 0.5.4, probably one of the many bug fixes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top