找不到R.layout.activity_main

时间:2022-08-12 21:12:31

I was trying to work out with multiple layout using some online tutorials. The thing is my program was building and compiling correctly as long as there was only one XML file. When I added multiple XML files, I am getting errors saying 'activity_main cannot be resolved or is not a field' for the line setContentView(R.layout.activity_main);

我试着使用一些在线教程来解决多重布局问题。问题是,只要只有一个XML文件,我的程序就能正确地编译和编译。当我添加多个XML文件时,会出现错误,说“activity_main不能被解析,或者不是为行setContentView(R.layout.activity_main)的字段”;

In fact, every item which I used previously in R.* is now reporting error. I don't know what went wrong. Can anyone suggest what needs to be done?

事实上,我以前在R中使用过的每一项。*现在报告错误。我不知道哪里出了问题。有人能提出需要做些什么吗?

Edit 1: I tried cleaning and rebuilding the project but that didn't help. Plus, I am importing the correct R.java file as in mypackage.R.java

编辑1:我尝试了清理和重建项目,但这并没有帮助。另外,我导入正确的R。java文件,如mypackage.R.java。

According to @RubberDuck's comment, I as soon as I deleted the files again, the R.java file was being generated after building the project. However, when I create new XML files and add elements in it, build the project, R.java gets deleted.

根据@RubberDuck的评论,当我再次删除文件时,R。java文件是在构建项目之后生成的。但是,当我创建新的XML文件并在其中添加元素时,构建项目R。java被删除。

20 个解决方案

#1


7  

If your XML's contain errors your generated file might not build try erasing most of their content and making sure id's and file names are not duplicated and no capitals and spaces are used. If it works you can start adding stuff.

如果您的XML包含错误,您生成的文件可能不会尝试删除它们的大部分内容,并确保id和文件名没有重复,并且不会使用任何大写和空格。如果可以的话,你可以开始添加一些东西。

#2


36  

I had the same problem, fixed by replacing R with com.example.appname.R obviously put your package reference in there instead.

我有同样的问题,用com.example.appname代替R。很明显,你把你的包引用放在那里了。

No idea what causes this, it just happened one day

不知道是什么原因造成的,只是有一天发生了。

#3


12  

Check if you not imported android.R accidentally.

检查你是否输入了android。R意外。

#4


3  

You're importing invalid R class, import yourpackage.R class for example com.example.R

您正在导入无效的R类,导入您的包。例如,R类。

what actully happends that u import android.R class not yourpackages.R

你在进口android时发生了什么事?R类不是yourpackages.R

#5


3  

Ivwd simply removed line "import android.R;" and call Project->Clean... That was enough for my on Win-7/x64 and Eclipse Kepler and last (02.2014) Android SDK

Ivwd只是删除了“import android.R”和call Project->Clean…这对于我的Win-7/x64和Eclipse Kepler和last (02.2014) Android SDK来说已经足够了。

#6


3  

The stupid mistake I made to cause this error was to have the wrong package string in my AndroidManifest.xml file. I'd left the string set in the tutorial, but had used my own when setting up the project.

我犯了一个愚蠢的错误,导致这个错误在我的AndroidManifest中有错误的包字符串。xml文件。我在教程中留下了字符串,但在设置项目时使用了自己的字符串。

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

#7


0  

You're importing invalid R class, check imports.

您正在导入无效的R类,检查导入。

#8


0  

There is probably an issue with the layout files, correct it and the auto generated class will appear.

布局文件可能有问题,纠正它,自动生成的类就会出现。

#9


0  

I have a few suggestions:

我有一些建议:

  1. Make sure you don't have any other errors other than the R-related errors. Right-click your project folder in Eclipse, Android Tools -> Fix Project Properties.

    确保您没有其他错误,除了与r相关的错误。在Eclipse中右键单击您的项目文件夹,Android Tools ->修复项目属性。

    2.Check to make sure you have the correct R imported. Sometimes the default Android.R can be imported.

    2。检查确保您有正确的R导入。有时候默认Android。R可以导入。

    3.Check for errors in your layout XML files.

    3所示。检查布局XML文件中的错误。

#10


0  

Thanks to @RubberDuck's comment and @Emil's answer I was able to figure out what the problem was. The ID's of most elements in my XML file were exactly the same. So, I renamed each and everyone of them. Also, my XML file contained capital alphabets. The filename should be in [a-z0-9_] so I renamed my files too and the problem was solved.

感谢@RubberDuck的评论和@Emil的回答,我能够找出问题所在。在我的XML文件中,大多数元素的ID是完全相同的。所以,我把每个人都重新命名了。而且,我的XML文件包含了大写字母。文件名应该在[a-z0-9_]中,所以我也重新命名了我的文件,问题就解决了。

#11


0  

This solved my problem,

这解决了我的问题,

apt-get install ia32-libs

and restart :)

并重启:)

Android adb not found

Android亚行未找到

#12


0  

or you can write

或者你可以写

import.R.layout // at the top

import.R。布局//在顶部。

It will work for sure

它肯定会起作用。

#13


0  

I used maven as build tool, after I build my project and dig a lot in the internet to solution. My problem resolved by adding to the main_activity.xml

我使用maven作为构建工具,在我构建项目之后,在internet上挖掘大量的解决方案。我的问题通过添加到main_activity.xml来解决。

tools:context=".MainActivity"

工具:上下文= " .MainActivity "

#14


0  

goto tools->android->Sync project with gradle file then click on S

goto工具->android->同步项目与gradle文件,然后点击S。

#15


0  

I've got this when an inappropriate character (/-one slash) was added to the values/strings.xml, and, of course, renaming of problematic resource was helpful. And one more important thing - check the date and time on your device.

当一个不合适的字符(/-一个斜杠)被添加到值/字符串时,我就得到了这个。当然,对有问题的资源进行重命名是有帮助的。还有一件更重要的事情——检查你设备上的日期和时间。

#16


0  

JUST REMOVE the import.android.R;

只是删除import.android.R;

Then the R.layout.(XML LAYOUTNAME) will turn to BLUE

然后出来。(XML LAYOUTNAME)将变为蓝色。

IT TOOK ME HOURS TO FIND THIS ONE ERROR

我花了好几个小时才找到这个错误。

#17


0  

I had a similar problem.
It happened suddenly, after deleting some pictures from res. drawable. I did two things.

我也有类似的问题。这是突然发生的,删除了一些图片。我做了两件事。

  • First I checked all my images in the drawable folder,if they all have preview. one of mine was damaged.
  • 首先,我在drawable文件夹中检查了所有的图像,如果它们都有预览。我的一个坏了。
  • Secondly, I closed all opened android studio and emulator. After that every things worked fine.
  • 其次,我关闭了所有开放的android studio和模拟器。之后,一切都很顺利。

#18


0  

In android Studio GO To

在安卓工作室。


tools---->>>>>>android------->>>>>Sync project with gradle Files

工具- - > android - - - - - - - > > > > > > > > > >项目与gradle文件同步


#19


0  

Happened once. Don't know the reason.

发生了一次。不知道的原因。

Build> Clean Project removed the error for me.

构建> Clean项目为我移除错误。

#20


-1  

I removed import android.R and added import android.support.v7.appcompat.*;

我删除导入安卓。R和添加导入android.support.v7.appcompat.*;

However this had to be done after importing appcompat by right click project and import

然而,这必须在导入appcompat之后通过右键单击项目和导入完成。

#1


7  

If your XML's contain errors your generated file might not build try erasing most of their content and making sure id's and file names are not duplicated and no capitals and spaces are used. If it works you can start adding stuff.

如果您的XML包含错误,您生成的文件可能不会尝试删除它们的大部分内容,并确保id和文件名没有重复,并且不会使用任何大写和空格。如果可以的话,你可以开始添加一些东西。

#2


36  

I had the same problem, fixed by replacing R with com.example.appname.R obviously put your package reference in there instead.

我有同样的问题,用com.example.appname代替R。很明显,你把你的包引用放在那里了。

No idea what causes this, it just happened one day

不知道是什么原因造成的,只是有一天发生了。

#3


12  

Check if you not imported android.R accidentally.

检查你是否输入了android。R意外。

#4


3  

You're importing invalid R class, import yourpackage.R class for example com.example.R

您正在导入无效的R类,导入您的包。例如,R类。

what actully happends that u import android.R class not yourpackages.R

你在进口android时发生了什么事?R类不是yourpackages.R

#5


3  

Ivwd simply removed line "import android.R;" and call Project->Clean... That was enough for my on Win-7/x64 and Eclipse Kepler and last (02.2014) Android SDK

Ivwd只是删除了“import android.R”和call Project->Clean…这对于我的Win-7/x64和Eclipse Kepler和last (02.2014) Android SDK来说已经足够了。

#6


3  

The stupid mistake I made to cause this error was to have the wrong package string in my AndroidManifest.xml file. I'd left the string set in the tutorial, but had used my own when setting up the project.

我犯了一个愚蠢的错误,导致这个错误在我的AndroidManifest中有错误的包字符串。xml文件。我在教程中留下了字符串,但在设置项目时使用了自己的字符串。

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

#7


0  

You're importing invalid R class, check imports.

您正在导入无效的R类,检查导入。

#8


0  

There is probably an issue with the layout files, correct it and the auto generated class will appear.

布局文件可能有问题,纠正它,自动生成的类就会出现。

#9


0  

I have a few suggestions:

我有一些建议:

  1. Make sure you don't have any other errors other than the R-related errors. Right-click your project folder in Eclipse, Android Tools -> Fix Project Properties.

    确保您没有其他错误,除了与r相关的错误。在Eclipse中右键单击您的项目文件夹,Android Tools ->修复项目属性。

    2.Check to make sure you have the correct R imported. Sometimes the default Android.R can be imported.

    2。检查确保您有正确的R导入。有时候默认Android。R可以导入。

    3.Check for errors in your layout XML files.

    3所示。检查布局XML文件中的错误。

#10


0  

Thanks to @RubberDuck's comment and @Emil's answer I was able to figure out what the problem was. The ID's of most elements in my XML file were exactly the same. So, I renamed each and everyone of them. Also, my XML file contained capital alphabets. The filename should be in [a-z0-9_] so I renamed my files too and the problem was solved.

感谢@RubberDuck的评论和@Emil的回答,我能够找出问题所在。在我的XML文件中,大多数元素的ID是完全相同的。所以,我把每个人都重新命名了。而且,我的XML文件包含了大写字母。文件名应该在[a-z0-9_]中,所以我也重新命名了我的文件,问题就解决了。

#11


0  

This solved my problem,

这解决了我的问题,

apt-get install ia32-libs

and restart :)

并重启:)

Android adb not found

Android亚行未找到

#12


0  

or you can write

或者你可以写

import.R.layout // at the top

import.R。布局//在顶部。

It will work for sure

它肯定会起作用。

#13


0  

I used maven as build tool, after I build my project and dig a lot in the internet to solution. My problem resolved by adding to the main_activity.xml

我使用maven作为构建工具,在我构建项目之后,在internet上挖掘大量的解决方案。我的问题通过添加到main_activity.xml来解决。

tools:context=".MainActivity"

工具:上下文= " .MainActivity "

#14


0  

goto tools->android->Sync project with gradle file then click on S

goto工具->android->同步项目与gradle文件,然后点击S。

#15


0  

I've got this when an inappropriate character (/-one slash) was added to the values/strings.xml, and, of course, renaming of problematic resource was helpful. And one more important thing - check the date and time on your device.

当一个不合适的字符(/-一个斜杠)被添加到值/字符串时,我就得到了这个。当然,对有问题的资源进行重命名是有帮助的。还有一件更重要的事情——检查你设备上的日期和时间。

#16


0  

JUST REMOVE the import.android.R;

只是删除import.android.R;

Then the R.layout.(XML LAYOUTNAME) will turn to BLUE

然后出来。(XML LAYOUTNAME)将变为蓝色。

IT TOOK ME HOURS TO FIND THIS ONE ERROR

我花了好几个小时才找到这个错误。

#17


0  

I had a similar problem.
It happened suddenly, after deleting some pictures from res. drawable. I did two things.

我也有类似的问题。这是突然发生的,删除了一些图片。我做了两件事。

  • First I checked all my images in the drawable folder,if they all have preview. one of mine was damaged.
  • 首先,我在drawable文件夹中检查了所有的图像,如果它们都有预览。我的一个坏了。
  • Secondly, I closed all opened android studio and emulator. After that every things worked fine.
  • 其次,我关闭了所有开放的android studio和模拟器。之后,一切都很顺利。

#18


0  

In android Studio GO To

在安卓工作室。


tools---->>>>>>android------->>>>>Sync project with gradle Files

工具- - > android - - - - - - - > > > > > > > > > >项目与gradle文件同步


#19


0  

Happened once. Don't know the reason.

发生了一次。不知道的原因。

Build> Clean Project removed the error for me.

构建> Clean项目为我移除错误。

#20


-1  

I removed import android.R and added import android.support.v7.appcompat.*;

我删除导入安卓。R和添加导入android.support.v7.appcompat.*;

However this had to be done after importing appcompat by right click project and import

然而,这必须在导入appcompat之后通过右键单击项目和导入完成。