Android/Eclipse 问题 - “id 无法解析或不是字段”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6011723/
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
Android/Eclipse Question - "id cannot be resolved or is not a field" error
提问by Makar
Hello I started trying to develop Android applications today, but I have a problem:
你好我今天开始尝试开发Android应用程序,但是我遇到了一个问题:
I decided to try out making a Web View using this tutorial - http://developer.android.com/resources/tutorials/views/hello-webview.html
我决定尝试使用本教程制作 Web 视图 - http://developer.android.com/resources/tutorials/views/hello-webview.html
But when I put the code in for the OnCreate() method I get an "id connot be resolved or is not a field" error. Here is my code:
但是,当我将代码放入 OnCreate() 方法中时,出现“无法解析 ID 或不是字段”错误。这是我的代码:
main.xml:
主文件:
<?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"
/>
HelloWebView.java:
你好WebView.java:
package com.example.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");
}
}
I have tried cleaning, CTRL+SHIFT+O, and just completely restarting the project. There error is in the statement: mWebView = (WebView) findViewById(R.id.webview); and Eclipse just says "id cannot be resolved or is not a field"
我试过清理,CTRL+SHIFT+O,然后完全重新启动项目。语句中有错误: mWebView = (WebView) findViewById(R.id.webview); Eclipse 只是说“id 无法解析或不是字段”
Also, I installed everything today using the guide on developer.android.com and followed all steps correctly. I have made Java programs on this computer before so I don't think there is a problem related to the JDK/JRE.
此外,我今天使用 developer.android.com 上的指南安装了所有内容,并正确执行了所有步骤。我之前在这台电脑上做过Java程序,所以我认为没有与JDK/JRE相关的问题。
回答by Makar
I managed to fix the problem by simply restarting my computer. I guess in order for all the development tools to work properly a computer restart is necessary? I can't believe I didn't think about doing this before though. Thanks for those who tried to help.
我通过简单地重新启动计算机设法解决了这个问题。我想为了让所有开发工具正常工作,需要重启计算机吗?我不敢相信我之前没有考虑过这样做。感谢那些试图提供帮助的人。
回答by Aleadam
There are a couple of similar questions here. Take a look at some answers:
这里有几个类似的问题。看看一些答案:
import of android.R : this response: R cannot be resolved - Android error
java tools not building R.java: R cannot be resolved - Android error
problem with default.properties file: R cannot be resolved - Android error
and finally, something reported by several: make sure the file is main.xml and not Main.xml
android.R 的导入:此响应:R 无法解析 - Android 错误
未构建 R.java 的 Java 工具:无法解析 R - Android 错误
default.properties 文件的问题:R 无法解析 - Android 错误
最后,几个报告的内容:确保文件是 main.xml 而不是 Main.xml
回答by sunami
Found the solution that finally worked for me after researching hours. Turns out the problem is not your R.java, All you gotta do is 'Clean' the project one last time. Save your project. And open Eclipse again. The problem is GONE!
经过几个小时的研究,找到了最终对我有用的解决方案。原来问题不是你的 R.java,你要做的就是最后一次“清理”项目。保存您的项目。并再次打开 Eclipse。问题消失了!
回答by Karthik
Look into the R File Generated and see whether you see a Public Static Class called Id if so then see whether it contains a public static final int webview ( that you have created )
查看生成的 R 文件,看看您是否看到一个名为 Id 的公共静态类,如果是,则查看它是否包含一个公共静态最终 int webview(您已创建)
回答by rochdev
I also get that from time to time.
我也时不时地得到它。
This usually (or always) does it for me: Project - Clean - "your project"
这通常(或总是)为我做:项目 - 清理 - “你的项目”