简体   繁体   English

如何修复程序包R不存在setContentView(R.layout.activity_main)及其后果?

[英]How to fix package R does not exist setContentView(R.layout.activity_main) and it consequences?

At first the error message was : 最初,错误消息是:

error: package R does not exist setContentView(R.layout.activity_main); 错误:包R不存在setContentView(R.layout.activity_main);

then I read a lot of answers in the StackOverflow about this problem so I tried to check the package name in the MainActivity and also the manifest file and then imported mypackage.R, then the problem message becomes like that: 然后我在StackOverflow中读到了很多有关此问题的答案,因此我尝试检查MainActivity和清单文件中的程序包名称,然后导入mypackage.R,然后问题消息变为:

    Creating myapp-debug-unaligned.apk and signing it with a debug key...
C:\Users\m0unta\AppData\Local\Android\android-sdk\tools\ant\build.xml:960: The following error occurred while executing this line:
C:\Users\m0unta\AppData\Local\Android\android-sdk\tools\ant\build.xml:971: The following error occurred while executing this line:
C:\Users\m0unta\AppData\Local\Android\android-sdk\tools\ant\build.xml:314: 
java.lang.NoClassDefFoundError: Could not initialize class sun.security.x509.X500Name
    at com.android.sdklib.internal.build.SignedJarBuilder.writeSignatureBlock(SignedJarBuilder.java:384)
    at com.android.sdklib.internal.build.SignedJarBuilder.close(SignedJarBuilder.java:273)
    at com.android.sdklib.build.ApkBuilder.sealApk(ApkBuilder.java:795)
    at com.android.ant.ApkBuilderTask.execute(ApkBuilderTask.java:371)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at com.android.ant.IfElseTask.execute(IfElseTask.java:124)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at com.android.ant.IfElseTask.execute(IfElseTask.java:124)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.GeneratedMethodAccessor269.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:286)
    at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:555)
    at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
BUILD FAILED (total time: 2 seconds)

Note: I am using Netbeans, and not Android Studio (maybe this is the problem, I don't know ) 注意:我使用的是Netbeans,而不是Android Studio(也许这是问题,我不知道)

R.java is generated when building your app. R.java是在构建应用程序时生成的。 The first thing to do to try to solve this error is to clean and rebuild your project. 解决此错误的第一件事是清理并重建项目。 If you still get the same error, then check all XML files. 如果仍然出现相同的错误,请检查所有XML文件。 Errors in AndroidManifest.xml and any layout or other XML resource files will stop generation of R.java. AndroidManifest.xml和任何布局或其他XML资源文件中的错误将停止R.java的生成。

I strongly suggest that you switch to Android Studio since it is the officially supported IDE. 我强烈建议您切换到Android Studio,因为它是受官方支持的IDE。

It's exactly like @Code-Apprentice said: I had made a new project by copying files from old project. 就像@ Code-Apprentice所说:我通过复制旧项目中的文件创建了一个新项目。

Tried to run: Package R does not exist . 尝试运行: Package R does not exist

I did Clean and Rebuild , twice; 我做了两次CleanRebuild still had the error. 仍然有错误。

I swear to you that I checked every xml file - AndroidManifest, ic_launcher, activity_main, colors, dimens, strings - but saw no problem, so I ran. 我向您保证,我检查了每个xml文件AndroidManifest, ic_launcher, activity_main, colors, dimens, strings -但没有发现问题,所以我就跑了。

Same error, of course; 当然,同样的错误; I'd changed nothing. 我什么都没改变。 Considered exiting and restarting Studio. 考虑退出并重新启动Studio。 Nah. 罗。 As Rocky said to Bullwinkle who was about to try to pull a rabbit from a hat, "That trick never works." 正如洛基对即将尝试从帽子上拉出兔子的Bullwinkle所说的那样,“这种技巧永远行不通。” (But sometimes it does.) (但有时确实如此。)

So my brain finally fired up an idea: I did a global project search for the old package name and found it in 3 places, one of which I could change: in AndroidManifest.xml . 因此,我的大脑终于产生了一个主意:我在全局项目中搜索了旧的package名称,并在3个地方找到了它,我可以更改其中之一:在AndroidManifest.xml I had changed one, but not the other occurrence of the old package name. 我更改了一个,但没有更改其他旧package名称。

PS I had wondered why I was getting a "can't find" sort of message on this line: PS我想知道为什么在此行上收到“找不到”消息:

    <activity android:name=".MainActivity">

I changed it to com.dslomer64.volumecontrolssbplus.MainActivity and no other error occurred at the time, but then I ran and got the "R" error. 我将其更改为com.dslomer64.volumecontrolssbplus.MainActivity ,当时没有发生其他错误,但随后我运行并得到了“ R”错误。 (I SHOULD have smelled a rat and looked closely right then at the rest of AndroidManifest .) (我应该闻到一只老鼠的气味,然后仔细看一下其余的AndroidManifest 。)

The Moral: Look closely at all xml (especially AndroidManifest ?) and then look closer and if you still don't see the problem, do a global project search for the old package name, with options "match case" and "whole word". 道德:仔细查看所有xml (尤其是AndroidManifest吗?),然后仔细查看,如果仍然看不到问题,请在全局项目中搜索旧程序包名称,并带有“区分大小写”和“全字”选项。

mention 提到

import com.projectdir.subdir.R;

in your java file.. 在您的Java文件中。

it needs proper path to access R 它需要正确的路径来访问R

....mine worked after this .... mine在此之后工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM