Android setContentView(R.layout.main); 从一开始就报错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10904925/
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
setContentView(R.layout.main); Gives error from start
提问by Mike
I'm just starting to develop with android, and a followed a tutorial on google to get everything set up. I'm using eclipse and I have both the android sdk and the eclipse plugin installed, but when I create a new Android Project, I get an error on this line from the start.
我刚刚开始使用 android 进行开发,并遵循了 google 上的教程来设置所有内容。我正在使用 eclipse 并且我已经安装了 android sdk 和 eclipse 插件,但是当我创建一个新的 Android 项目时,我从一开始就在这条线上收到错误消息。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
It Says that the "R" in "R.layout.main" does not exist. Is there class that i need to create or import? I'm really confused because i have thoroughly searched and cannot find anyone else with this issue.
它说“R.layout.main”中的“R”不存在。是否有我需要创建或导入的类?我真的很困惑,因为我已经进行了彻底的搜索,但找不到其他有此问题的人。
回答by android developer
this is quite a common mistake . make sure that at the top of your java file of the activity , the import is not of android.R.layout (or whatever) ,since it should import the R of your own app (which is inside the gen folder).
这是一个很常见的错误。确保在活动的 java 文件的顶部,导入不是 android.R.layout (或其他),因为它应该导入您自己的应用程序的 R (在 gen 文件夹内)。
if you still have problems with that , try to delete the gen folder , make sure the layout file is ok (meaning it's a correct android layout file, without any mistakes), make sure all of the resources (all images, sounds , xml files , all that are in /res) have only the legal characters in their files names (which means they are lowercase english ,optionally with "_" and digits, but no more) , clean the project , and then import the generated file .
如果您仍然有问题,请尝试删除 gen 文件夹,确保布局文件正常(意味着它是正确的 android 布局文件,没有任何错误),确保所有资源(所有图像、声音、xml 文件) ,所有在 /res 中的文件名中只有合法字符(这意味着它们是小写英文,可选带有“_”和数字,但没有更多),清理项目,然后导入生成的文件。
you can also use CTRL+1 on the red underline of the R that it complains about , and choose the one of your project.
你也可以在它抱怨的 R 的红色下划线上使用 CTRL+1 ,然后选择你的项目之一。
回答by chip
Bad resources can cause your android project not to generate R.java file. Try to check /res/layout and see if there are errors on anyof your layouts.
不良资源会导致您的 android 项目无法生成 R.java 文件。尝试检查 /res/layout 并查看您的任何布局是否有错误。
Also try to get rid of import android.R.*;
也尝试摆脱 import android.R.*;
Then go to Project > Clean
然后去项目>清洁
回答by Abdul Kabani
Add main.xml in res->layout main.xml
在 res->layout main.xml 中添加 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
OR Layout->New-android XML file
或 Layout->New-android XML 文件
clean and Build Project
清理和构建项目
回答by Pawan Tanwar
You can import the package like so:
您可以像这样导入包:
import com.example.kaushal.checkbox.R;
and then rebuild your project.
然后重建你的项目。
回答by Aditya
just go to build option and click on rebuild project. This might work. :)
只需转到构建选项并单击重建项目。这可能会奏效。:)
回答by Alex Lockwood
Right click your project and click Fix Project Properties.
右键单击您的项目,然后单击Fix Project Properties。
If that doesn't work, then you either have an error somewhere in your project that is preventing your R.java
class from being generated, or eclipse is being dumb and you need to perform a Project --> Cleanand/or restart the IDE all together.
如果这不起作用,那么您要么在项目中某处出现错误,导致您的R.java
类无法生成,要么 eclipse 很笨,您需要执行一个项目 --> 一起清理和/或重新启动 IDE .
回答by bhekman
Usually a main layout file is auto-generated for you when you create an Android project.
通常,当您创建 Android 项目时,会自动为您生成一个主布局文件。
Make sure that you have a file named "main.xml" inside of your res/layouts folder. Assuming that you do, try Project->Clean.
确保在 res/layouts 文件夹中有一个名为“main.xml”的文件。假设您这样做了,请尝试 Project->Clean。
回答by vidit
i had also getting this issue just import package as your previous. Example if your main page package name is import com.example.chephotel.R; just copy and paste same in your newly created java file
我也遇到了这个问题,只是像以前一样导入包。例如,如果您的主页包名称是 import com.example.chephotel.R; 只需将相同的内容复制并粘贴到您新创建的 java 文件中