简体   繁体   English

为什么找不到我创建的 MainActivity.java 文件的布局?

[英]Why can't i find the layout I have created my MainActivity.java file?

I was following the instructions online in android app development course for beginners and I encountered this problem.我是按照 android 初学者应用程序开发课程中的在线说明操作的,我遇到了这个问题。

The instructor was demonstrating how we could use the layouts we created for the activity.讲师正在演示如何使用我们为活动创建的布局。 He was able to find the layout he created using R.layout.他能够找到他使用R.layout. , but I could not. ,但我做不到。

The activity_main_listview.xml is the View that I created. activity_main_listview.xml是我创建的视图。 I am an absolute beginner so I don't know what is wrong here.我是一个绝对的初学者,所以我不知道这里有什么问题。

Is there any way I could resolve this problem?有什么办法可以解决这个问题吗?

I have tried to Synchronize both the .xml files and the MainActivity.java file.我试图同步.xml文件和MainActivity.java文件。 I have also tried to clean and rebuild the project.我也尝试清理和重建项目。

This is a picture of the part where it did not work这是它不起作用的部分的图片

I had the same problem, fixed by replacing R with com.example.appname.R obviously put your package reference in there instead. I had the same problem, fixed by replacing R with com.example.appname.R obviously put your package reference in there instead. Or just add this line to your file:或者只是将此行添加到您的文件中:

import com.testapp.activitydemo.package.R

Or even better, try removing this line from your code if exists:或者更好的是,如果存在,请尝试从代码中删除此行:

import android.support.compat.R



If your XML's contain errors your generated file might not build try erasing most of their content and making sure id's and file names are not duplicated and no capitals and spaces are used.如果您的 XML 包含错误,您生成的文件可能无法构建,请尝试删除它们的大部分内容并确保 id 和文件名不重复,并且不使用大写字母和空格。 If it works you can start adding stuff.如果它有效,你可以开始添加东西。

Another note:另一个注意事项:

Sync Project With Gradle File Once you update the Gradle plugin you need to sync project with the Gradle file.将项目与 Gradle 文件同步更新 Gradle 插件后,您需要将项目与 Gradle 文件同步。 Open android studio and click on Files > Sync Project with Gradle Files option.打开 android studio 并单击Files > Sync Project with Gradle Files 选项。

Clean and Rebuild Project The most effective solution is the simplest: clean and rebuild your project.清理并重建项目 最有效的解决方案是最简单的:清理并重建您的项目。 Select Build > Clean Project from the Android Studio toolbar, wait a few moments, and then build your project by selecting Build > Rebuild Project. Select 从 Android Studio 工具栏中构建 > 清理项目,稍等片刻,然后通过选择构建 > 重建项目来构建您的项目。

This is common problem which occurs with Android Studio!这是 Android Studio 出现的常见问题!

Its not your problem, it is bug of Android Studio!不是你的问题,是 Android Studio 的 bug!

To resolve this, Just Clean your project and Restart Android Studio!要解决此问题,只需清理您的项目重新启动 Android Studio!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM