Unable to set parent activity for new activities in Android Studio. Error: Hierarchical parent must already exist
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.
3 Solutions collect form web for “Unable to set parent activity for new activities in Android Studio. Error: Hierarchical parent must already exist”
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:
- 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.
- 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!
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.