无法从Android Studio的主活动访问XML表单

huangapple go评论74阅读模式
英文:

Can't Access XML form Main Activity in Android Studio

问题

我无法从主活动访问我的 XML 表单中的元素,写入元素 ID 会抛出错误,就像元素根本不存在一样。基本上,主要无法看到 XML 文件中的内容,尽管它们是链接在一起的。

英文:

I cannot access the elements in my XML form the main activity,
writing the element id throws errors as if the element is not even there.
Basically Main can't see what is written in XML file, even though they are linked.

Can't detect the getBmiButton button from XML

Button was defined here

答案1

得分: 6

你不再需要使用findViewById()。
你应该通过将以下代码添加到build.gradle(app)来集成Kotlin Android扩展:

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

然后可以将你的视图作为属性使用。如果你对其工作原理感到好奇,这篇文章会对你有所帮助。

英文:

you Don't need findViewById() anymore.
You should Integrate Kotlin Android Extensions by adding these lines to build.gradle (app)

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

and use your view as a property. If you are curious how this works, this article will help you.

答案2

得分: 0

要直接访问XML视图的标识符以及代码,您可以选择使用DataBindingViewBindingKotlin Extensions依赖项之一,但目前您没有使用其中任何一个。默认情况下,R.java是由aapt(Android资源打包工具)自动生成的文件,其中包含了res/目录中所有资源的资源ID。

您可以按照以下方式访问这些标识符:

val button = findViewById(R.id.getBmiButton) as Button

干杯 无法从Android Studio的主活动访问XML表单

英文:

To access the XML view's Identifiers directly into the code, you can choose among DataBindinng, ViewBinding or Kotlin Extensions dependencies but currently, you're not using any one of these. By default, R.java is an auto-generated file by aapt (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory.

you can access the identifiers as follow:

val button = findViewById(R.id.getBmiButton) as Button

Cheers 无法从Android Studio的主活动访问XML表单

答案3

得分: 0

前往菜单栏中的“Build”下拉菜单。在“Clean Project”下方,您可以看到“Rebuild Project”。重新构建项目实际上会清理项目。如果找不到“Rebuild Project”,您可以直接使用“Clean Project”。

英文:

Go to the Build drop down menu in the menu bar. you can see the Rebuild Project under the Clean Project. Rebuild project cleans the project actually. If you cant find Rebuild Project you can simply use Clean Project.

答案4

得分: -1

button = findViewById(R.id.getBmiButton) as Button;

访问这里

英文:
button = findViewById(R.id.getBmiButton) as Button;

visit here

huangapple
  • 本文由 发表于 2020年10月25日 14:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64521000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定