Java 带有 NBAndroid 的 NetBeans - 找不到符号 setContentView(R.layout.main);

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9799927/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:47:33  来源:igfitidea点击:

NetBeans with NBAndroid - cannot find symbol setContentView(R.layout.main);

javaandroidnetbeans

提问by Daniel Harris

I recently installed NetBeans 7.1.1 with NBAndroid 1.11 plugin. After starting a new HelloWorld project, I get the following error from the test code:

我最近安装了带有 NBAndroid 1.11 插件的 NetBeans 7.1.1。在启动一个新的 HelloWorld 项目后,我从测试代码中得到以下错误:

C:\Users\Daniel\Documents\NetBeansProjects\HelloWorld\src\Hello\World\HelloWorldMain.java:14: error: cannot find symbol setContentView(R.layout.main);

C:\Users\Daniel\Documents\NetBeansProjects\HelloWorld\src\Hello\World\HelloWorldMain.java:14: 错误:找不到符号 setContentView(R.layout.main);

Here is my source code for HelloWorldMain.java file:

这是我的 HelloWorldMain.java 文件的源代码:

package Hello.World;

import android.R;
import android.app.Activity;
import android.os.Bundle;

public class HelloWorldMain extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

The setContentView function is causing the problem for some reason. I checked the R.java file for any errors and didn't seem to find any:

由于某种原因, setContentView 函数导致了这个问题。我检查了 R.java 文件是否有任何错误,但似乎没有找到任何错误:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package Hello.World;

public final class R {
    public static final class attr {
    }
    public static final class layout {
        public static final int main=0x7f020000;
    }
    public static final class string {
        public static final int app_name=0x7f030000;
    }
}

Any help would be appreciated.

任何帮助,将不胜感激。

回答by MByD

You need to remove import android.R;. Your Rand android.Rare two different classes.

您需要删除import android.R;. 你Randroid.R是两个不同的类。

回答by Pavul

In Android, we dont have a class that is auto-generated, so, if you doesnt find your R class try to build your project or to run it. After that you can find that a new package was added to your project, there is where you can find your R class.

在 Android 中,我们没有自动生成的类,因此,如果您没有找到 R 类,请尝试构建您的项目或运行它。之后你会发现一个新的包被添加到你的项目中,你可以在那里找到你的 R 类。