Android 修复 style.xml 中的错误以生成 R.java:找不到资源名称“Theme.AppCompat.Light”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22271759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:46:14  来源:igfitidea点击:

Fixing Error in styles.xml to generate R.java : No resource found name 'Theme.AppCompat.Light'

androidxmlandroid-support-libraryr.java-fileandroid-appcompat

提问by ravensgo

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.Rbecause r.javahas 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.xmlis

我是 Android 新手,正在尝试运行我的第一个程序。但是,根据我在互联网上的搜索,我认为我无法导入,mypackage.R因为r.java我的 style.xml 文件中的错误导致尚未生成。我四处寻找试图找出如何解决这些问题的方法,但我找不到有效的解决方法。中的错误styles.xml

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]

![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:

更新:这里是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

更新 2:这是 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>

采纳答案by Hamid Shatu

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

您正在尝试使用Theme.AppCompat.Light主题,这是一个库项目。您必须将此库项目引用到您的项目中。

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

现在,首先,检查您是否已经安装了这个库项目,如下所示......

Go Window-->Android SDK Managerthen a window named Android SDK Managerwill appear as below.

Go Window--> Android SDK Manager然后会出现一个名为Android SDK Manager的窗口,如下所示。

enter image description here

在此处输入图片说明

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

如果Android Support Library未安装,则安装它。您可以Android Support Library从下面的 Android 开发人员站点查看有关设置的更多信息。

Support Library Setup

支持库设置

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

Android Support Library安装完成后,引用该库到您的项目从此路径...

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"
  1. 文件->导入(android-sdk\extras\android\support\v7)。选择“ appcompat
  2. 项目->属性-> Android。在部分库中“添加”并选择“ appCompat

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

现在,清理并构建您的项目并运行它。我想在所有这些之后,您的问题将得到解决。

回答by skm9

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

我终于想通了问题所在。我不得不从我的main.xml以下代码中删除这行代码:

android:showAsAction="never"

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

在我的values-v11values-14文件夹下,我将应用程序的名称更改为...

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

...and it is currently working.

...它目前正在工作。

回答by Ye Lin Aung

Since you're using Theme.AppCompat.Lightin your Theme, you have to include appCompatinto your Project.

由于您Theme.AppCompat.Light在主题中使用,因此必须包含appCompat到您的项目中。

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

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

  • "Add" and choose "appCompat"

  • 文件->导入(android-sdk\extras\android\support\v7)。选择“应用兼容”

  • 项目-> 属性-> Android。在截面库中

  • “添加”并选择“appCompat”

That should work.

那应该工作。

回答by donfuxx

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

确保你<resources>正确关闭了标签并在你的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.

编辑:缺少的资源标签只是一个有问题的复制粘贴错误,所以这不是原因。