Search code examples
javaandroidxmlandroid-actionbarandroid-theme

Error inflating class com.android.internal.widget.ActionBarContainer


So, recently I had to move my project from where it was due to the Error: File path too long on windows, keep below 240 characters. After moving, and solving other errors when I run my project, the app crashes with android.view.InflateException: Binary XML file line #31: Error inflating class com.android.internal.widget.ActionBarContainer. Unfortunately, I cannot post the logcat message as it is too big to post, so I will just put some Caused by messages here. Apart from the inflate exception the other two exceptions are: Caused by: java.lang.reflect.InvocationTargetException and Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 13. All of the exceptions and the error itself refers to a class' setContentView(R.layout.activity_automatic_floor_plan_loader);. In this layout file I am using AppTheme.AppBarOverlay. I am quite sure everything relates to this apptheme, but I do not know what to change and how to. I have gone through many Stackoverflow questions, but none of them seems to answer or solve the error I am getting. Here is my Style.xml file: Style.xml

And here is my class' activity in the Android-Manifest.xml file: AndroidManifest.xml

And here is my activity.xml (activity_automatic_floor_plan_loader): activity.xml

And here is my build.gradle(app) file: Build.gradle How to solve this error? Many thanks.


Solution

  • Here is how I solved this error:

    1. In the styles.xml file, in the first "AppTheme" style I had to put Base infront of Theme.AppCompat. So it would be "Base.Theme.AppCompat.Light.DarkActionBar".

    2. Then I used android:theme="@style/AppTheme" in my parent activity in androidmanifest.xml.

    3. In the androidmanifest file in the child activity, I had to put android:parentActivityName=".parentActivity" and I removed android:theme="@style/AppTheme.AppBarOverlay" from the child activity and put it in the activity.xml (child activity) file as below.

    4. In the activity.xml file in the LinearLayout tag, I had to put android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar". It is the parent of the "@style/AppTheme.AppBarOverlay".

    This 4 step process worked for me, and if you are getting this error, just follow these steps and it should work for you as well.

    Edit: It turned out that the solution was very simple. To have an action bar in any of your activities, you just need to extend AppCompatActivity. Only my main activity was extending AppCompatActivity and not other activities. The above approach for getting an action bar is quite vague and I knew it, however, it worked initially, but now I have the correct and straight forward solution and if you are getting such error then just check that whether you are extending AppCompatActivity in your class or not.