1

I cannot explain nor fix this nagging error of "Top level element not completed" This is a brand new project as in just created.

Here the styles.xml screenshotenter image description here

And here is part of the Gradle file

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:support-v4:22.1.1'
}

So its not like its missing a reference. The only other thing in the app so far is ToolBar, what else am I missing to make this error go away, I am still able to build and run the app,but the red squiggly line remains, any ideas would be greatly appreciated.

Val Okafor
  • 3,371
  • 13
  • 47
  • 72

3 Answers3

2

first of all add this

compile 'com.android.support:appcompat-v7:22.0.0'  // 21 if your target is 21.

in your style.xml file create base theme.. This means that you have one base theme which will applicable in all version from 11 to 21 in your case. you can set theme as you want.

<style name="AppTheme" parent="AppTheme.Base">
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/primaryColor</item> <!--do not use android:colorPrimary : older version doesn't recognize it.-->
    <item name="colorPrimaryDark">@color/primaryColorDark</item>
    <item name="colorAccent">@color/accentColor</item> <!-- in base theme don't use android prefix. it is not supported -->
</style>

above all code in style.xml

Now, you have style.xml(v21) in that file you need to inherit that base theme.

<style name="AppTheme" parent="AppTheme.Base">
    <item name="android:colorPrimary">@color/primaryColor</item> <!--In version 21 you need to provide it as android:colorPrimary : older version only doesn't recognize it.-->
    <item name="android:colorPrimaryDark">@color/primaryColorDark</item>
    <item name="android:colorAccent">@color/accentColor</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:actionBarSize">60dp</item>
    <item name="android:textColor">#FFF</item>
</style>

This way, i hope you understand, AppTheme.Base will be applicable to all API. and plus in 21 it will use specifically with v21. in this way you can set your min sdk version lower as you wanted it to be 11.

Try this.! let me know if it works.!

Ajay P. Prajapati
  • 1,973
  • 1
  • 17
  • 34
  • I tried this, then it complained about my minimum target API so I increased from 11 to 21 and my target API level @ 22. I even created a values-v21 folder. Closed and re-opened the project with no success. I will restart Android Studio and the laptop nex. – Val Okafor May 29 '15 at 02:05
  • You only need to specify the "android" namespace for API 21 and up. If your style.xml is in the root style folder (not v21), then you don't need to do that. – hungryghost May 29 '15 at 02:11
  • 1
    I have updated my answer.. let me know it works or not... sorry for late reply was busy! – Ajay P. Prajapati May 29 '15 at 03:08
  • 1
    Thanks, I tried it without success, I ended up resetting my Android Studio to fix it. Thank you for your kind help I upvoted the answer – Val Okafor May 29 '15 at 05:53
1

It might be related to your style inheritance. Maybe try deleting the first AppTheme style, and changing your custom style like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">...</item>
</style>

Just make sure you reference this AppTheme name for you activities.

hungryghost
  • 9,463
  • 3
  • 23
  • 36
0

After toiling with this for a few hours, I started to suspect that my Android Studio installation or atleast my Android Studio editor is corrupt and my assumptions turned out correct.

Following instructions here I reset my Android Studio installation to defaults and voila the error went away. For the actual styling one does not have to create a seperate folder -v21 for lolipop devices. Per this post something like this will work.

If you are using AppCompat, then all of the material color palette attributes (such as colorPrimary) are available to all API levels, so you can write a single theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>
Community
  • 1
  • 1
Val Okafor
  • 3,371
  • 13
  • 47
  • 72