启动新的 Android 项目后,无法立即识别主布局

发布于 2025-01-03 04:51:45 字数 463 浏览 3 评论 0原文

当我开始一个新的android项目时,下面的代码是自动生成的。

我在“R.layout.main”上收到错误,说它不存在。

事实上,我确实有一个 main.xml,当我在“图形布局”选项卡中编辑它时,我可以看到布局发生变化。

我该如何解决这个问题以便我可以运行我的应用程序?

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

The below code was automatically generated when I started a new android project.

I'm getting an error on the "R.layout.main" saying it does not exist.

I do in fact have a main.xml and I can see the layout change as I edit it in the Graphical Layout tab.

How can I fix this so I can run my application?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丶情人眼里出诗心の 2025-01-10 04:51:45

R 文件的包名称和源代码的包名称可能不匹配。确保您的代码所在的包与清单文件中定义的包相同。否则,您将需要导入具有完整包名称的 R 文件(例如:com.example.R.layout.main)。

如果它们匹配,则由于某种原因您的 R 文件未正确生成。尝试清理您的项目。

The package name of the R file and the package name of your source code may not match. Make sure the package your code is in is the same as the package defined in the manifest file. Otherwise you will need to import the R file with the full package name (ex: com.example.R.layout.main).

If they match, for some reason your R file was not generated properly. Try cleaning your project.

很糊涂小朋友 2025-01-10 04:51:45

这可能是一个非常简单的答案,但我以前也遇到过这种情况。尝试重新启动 eclipse。文件->重新启动

This may be a very simple answer, but it has happened to me before. Try to restart eclipse. File -> Restart

夜雨飘雪 2025-01-10 04:51:45

尝试使用主活动类文件中的完整路径导入 R.java 文件...

import com.example.packagename.R;

希望这能解决您的查询。

Try to import the R.java file with the full path inside the main activity class file...

import com.example.packagename.R;

Hope, this will solve your query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文