Fixing Error in styles.xml to generate R.java : No

2019-01-09 02:06发布

问题:

I am new to Android and am trying to run my first program. However, based on my searches across the internet, I think that I can't import mypackage.R because r.java has not been generated because of errors in my style.xml files. I have searched around trying to figure out how to fix these but I can't find a fix that works. The error in styles.xml is

error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'

Does anyone know how to fix this?

![Errors in style.txt][1]

Here is the code I am using:

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

UPDATE: Here is styles.xml:

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

</resources>

UPDATE 2: Here is AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

回答1:

You are trying to use Theme.AppCompat.Light theme which is a library project. You have to reference this library project to your project.

Now, at first, check that you have installed this library project as follows...

Go Window-->Android SDK Manager then a window named Android SDK Manager will appear as below.

If the Android Support Library is not installed then install it. You can see more information about Android Support Library setup from the below Android Developer site.

Support Library Setup

After Android Support Library setup completion, reference the library to your project from this path...

android-sdk/extras/android/support/v7/appcompat

To reference, follow these steps:

  1. File->Import (android-sdk\extras\android\support\v7). Choose "appcompat"
  2. Project-> properties->Android. In the section library "Add" and choose "appCompat"

Now, clean and build your project and run it. I think after all of these, your problem will be solved.



回答2:

I finally figured out the problem. I had to delete this line of code from my main.xml:

android:showAsAction="never"

Under my values-v11 and values-14 folders I changed the name of the app to...

style name="AppBaseTheme" parent="android:Theme.Light"

...and it is currently working.



回答3:

Since you're using Theme.AppCompat.Light in your Theme, you have to include appCompat into your Project.

  • File->Import (android-sdk\extras\android\support\v7). Choose "appcompat"

  • Project-> properties->Android. In the section library

  • "Add" and choose "appCompat"

That should work.



回答4:

Make sure you closed the <resources> tag properly and add at the bottom of your styles.xml:

 </resources>

according to your posted code snippet that is not present, but needs to be

Edit: the missing resources tag was just a copy-paste bug in question, so here that was not the reason.