Java R.id 无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2483732/
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
R.id cannot be resolved
提问by Soren
So I copied this tutorial example thing right from Google's android site and I ma getting an error that R.id cannot be resolved.
所以我直接从 Google 的 android 站点复制了这个教程示例,但我收到了一个错误,即 R.id 无法解析。
Here is my Java file
这是我的 Java 文件
package com.TestApp.HelloWebView;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class HelloWebView extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
}
Here is my res/layout/main.xml
这是我的 res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
采纳答案by Pentium10
You have to import your R class
你必须导入你的 R 类
import com.TestApp.HelloWebView.R;
Also as Demand posted you have to use a namespace for your layout.
同样作为需求发布,您必须为您的布局使用命名空间。
?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
回答by Charles Duffy
If using Eclipse, run a clean build (Project/Clean...
); this can often clear issues with the resources not being compiled correctly.
如果使用 Eclipse,请运行干净的构建 ( Project/Clean...
);这通常可以清除资源未正确编译的问题。
回答by Alex Florescu
Did you try any of the solutions listed here? R cannot be resolved - Android error
您是否尝试过此处列出的任何解决方案? R 无法解析 - Android 错误
Also, look for your R.java file. Are there any errors with it?
另外,查找您的 R.java 文件。它有什么错误吗?
回答by lbedogni
Go in your main.xml file, delete a char and rewrite it. That will generate the R file again.
进入您的 main.xml 文件,删除一个字符并重写它。这将再次生成 R 文件。
回答by Alexander Mikhaylov
if you posted full your main.xml file, then it's not right. You must write a namespace of xml:
如果您发布了完整的 main.xml 文件,那就不对了。你必须写一个xml的命名空间:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
for example:
例如:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="78dip"
android:layout_height="65dip"
android:paddingTop="4dip"
android:textSize="12dip"
android:singleLine="true"
android:ellipsize="end"
android:textColor="@color/bright_text_dark_focused"
android:gravity="center_horizontal|center_vertical" />
回答by Arturski
make sure you do not have any xml files with capitals in the name.
确保您没有任何名称中带有大写字母的 xml 文件。
I had the same problem.
我有同样的问题。
all the file names in res (and all its sub folders including layout) must be lower case only. This includes image files, xml files, etc (even if they aren't referenced anywhere within the project.)
res 中的所有文件名(及其所有子文件夹,包括布局)只能小写。这包括图像文件、xml 文件等(即使它们没有在项目中的任何地方被引用。)
回答by pestrella
I had the same problem when I updated my Android SDK. Suddenly new projects could not generate the R.java file.
当我更新我的 Android SDK 时,我遇到了同样的问题。突然新项目无法生成 R.java 文件。
- importing my R.java didn't help because it wasn't generated
- cleaning and building the project didn't help either -- the /gen folder was still empty.
- 导入我的 R.java 没有帮助,因为它没有生成
- 清理和构建项目也没有帮助 - /gen 文件夹仍然是空的。
I found that after the upgrade, the adb server didn't start up properly. So a manually started it:
发现升级后adb服务器不能正常启动。所以手动启动它:
$ cd $ANDROID_SDK/platform-tools
$ adb start-server
Then I restarted Eclipse and did a clean/build on the project, which generated the R.java file.
然后我重新启动 Eclipse 并对项目进行了清理/构建,生成了 R.java 文件。
回答by ozmike
Yeah another fix I started the emulator. By running a helloandroid program, that did compile. And got out of eclispe and got back in don't know which one fixed the problem..
是的另一个修复我启动了模拟器。通过运行 helloandroid 程序,它确实可以编译。然后从 eclispe 出来又回来了,不知道是哪一个解决了问题..
Cleaning the project etc didn't fix it - can i have 2 hrs of my life back!
清理项目等并没有解决它 - 我可以恢复我的生活 2 小时吗!
回答by Paul
On trying an Android beginner's tute (http://mobile.tutsplus.com/tutorials/android/android-sdk-development/) the "R.id cannot be resolved" error had me stumped too. In my case it was an android:onClick in /res/layout/main.xml. It seems onClick is not supported in AVD platform 1.5 API level 3.
在尝试 Android 初学者教程 (http://mobile.tutsplus.com/tutorials/android/android-sdk-development/) 时,“R.id 无法解析”错误也让我难倒。就我而言,它是 /res/layout/main.xml 中的 android:onClick。似乎 AVD 平台 1.5 API 级别 3 不支持 onClick。
A change to a new AVD with platform 1.6 API, level 6 removed the error - also started the project from scratch with the new api and level settings. For absolute Eclipse newbies (like myself) create a new AVD with:
使用平台 1.6 API 对新 AVD 进行更改,级别 6 消除了错误 - 还使用新的 api 和级别设置从头开始了项目。对于绝对的 Eclipse 新手(比如我自己),创建一个新的 AVD:
Window -> Android SDK and AVD Manager -> New. Select platform, level number from "Target" drop down list.
窗口 -> Android SDK 和 AVD 管理器 -> 新建。从“目标”下拉列表中选择平台、级别编号。
回答by Andy
Also try deleting the ~.android\debug.keystore (Happened to me, thought it was due to R.id issue). Real problem was debug.keystore was older than a year!
也尝试删除 ~.android\debug.keystore (发生在我身上,以为是由于 R.id 问题)。真正的问题是 debug.keystore 已经超过一年了!