我刚刚下载并安装了新的Android SDK。我想创建一个简单的应用程序来测试它。
向导创建了以下代码:
package eu.mauriziopz.gps;
import android.app.Activity;
import android.os.Bundle;
public class ggps extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
但是Eclipse给出了错误
R不能被分解
在网上
setContentView(R.layout.main);
Why?
PS:我在res/layout/下有一个名为main.xml的XML文件。
R.java是Android Eclipse插件创建的文件
构建应用程序。R.java是在“gen”下创建的
目录中。此文件由“res”中的信息生成。
目录中。如果在Eclipse上运行select“Project”->“Clean…
菜单,它将删除,然后重新生成R.java文件。
“R不能被解决”的问题发生在你改变你的
在AndroidManifest.xml文件中的包名。它使用安卓系统
包名下创建子目录“gen”目录,其中
它存储R.java文件。
Eclipse may have problems executing clean, because it is confused about
where the R.java file is when you have changed the Android package
name. You can either rename the subdirectory under gen to match your
new package name, or you can change your package name back to the old
name. Do the clean and then change the package name to the new name
you want. This works best if you stop Eclipse from trying to build
while you are changing the package name. Under the "Project" menu
uncheck the option to "Build Automatically" and also when the
"Clean..." dialog asks if it should "Start a build immediately"
uncheck the box so it doesn't try to build while you are changing the
package name. After you have changed the name you can turn "Build
Automatically" back on again.
Note that if your AndroidManifest.xml file package name does not match
your Java package name, Eclipse will end up automatically adding an
"import <your Android package name>.R;" line in all your .java files
that have any references to R. If you change your AndroidManifest.xml
package name, sometimes Eclipse does not update all of these added
imports. If that happens, use the Eclipse refactoring (ALT +
Shift + R) to change the import statement in one of your Java files to
your new AndroidManifest.xml package name. It is best to do this
while you have disabled "Build Automatically".
So I have run into this problem multiple times when switching build targets. Usually doing a Project >> Clean worked for me. This last time, however, it did not. Finally I tried to open my default.properties file, located under the root project folder. I received an error message stating that it was out of sync with the file system. I actually deleted it and copied a coworkers version which allowed eclipse to rebuild my R file. I will paste what it looks like below. It is named 'default.properties'.
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.
# Indicates whether an apk should be generated for each density.
split.density=false
# Project target.
target=android-3
通常情况下,这是因为您在创建项目时提供的MinSDK版本号。例子:
如果你想把2.1作为最小值,Android 2.1实际上是API Level 7。
当你浏览下载并安装的SDK时,你就会明白我在说什么。导航到你安装SDK的地方(C:\android-sdk-windows为例),打开名为“平台”的文件夹。你会看到像“android-7”这样的东西被列为一个文件夹,如果你打开它,就会有一个源代码。属性文件,当用文本编辑器打开时,将显示相应的平台版本。
当你创建一个项目时,你必须选择一个“Build Target”API,该列表中名为“API Level”的最后一列显示了你在填充MinSDK设置时所寻找的数字。
这可能是导致R.java文件没有在Project > gen > packagename > R.java下创建的最常见错误之一。
I had tried all of the above with no prevail. It turned out PhoneGap was causing a conflict with the Android SDK. After uninstalling PhoneGap, the Resources file started regenerating again. Another project, needed PhoneGap so I re-installed, and once again, the Android project (a different project) stopped auto-generating the R file - Build Automatically was checked, did the Clean, and restarted Eclipse - no dice. I removed PhoneGap and it was working once again. This was PhoneGap v 1.5 with the MDS 1.1 plugin for Eclipse.
对我来说,这是因为使用了“私人”android drawables:
[2015-11-26 18:59:48 - MyAndroidApp]
/Users/myname/git/MyAndroidApp/res/menu/drawer_view.xml:24:
error: Error: Resource is not public. (at 'icon' with value
'@android:drawable/ic_menu_home').
有趣的是,直到我使用图标删除了对文件的引用,这个错误才出现在控制台中。
android:entries="@menu/drawer_view"
现在解决方案非常明显,将图标复制到我的项目(res/draw - xxxx /)并切换到本地引用。
@drawable/ic_menu_home
希望这能有所帮助。
R不能解决的原因有几个。
大多数情况下,此错误发生在XML文件错误。这个错误可以通过Clean和重建你的项目来解决,也可以重新启动你的android studio。
有时候如果你在AndroidManifest中编码一些错误的东西,这个错误可能会发生,所以请检查这个。
如果资源包含相同的名称或包含无效字符,则会发生此错误。
如果你已经检查了这些,但你仍然面临着同样的问题,那么继续:
当Android Studio无法正确生成R.java文件时,就会出现这个错误,所以你必须遵循以下步骤:
第一步:从app目录中安全删除build文件夹。
步骤2:在Android Studio中,File -> Invalidate cache / Restart
步骤3:构建->清洁项目然后构建->重建项目。
所以构建文件夹将重新生成,希望错误已经消失。
如果仍然发现Error,那么可能您之前已经执行了包名重构。在这种情况下,您可以按ALT + ENTER一起导入R。