Android ViewBinding不会生成类文件
我一直在关注这个 教程在这里关于如何执行视图绑定,似乎没有什么可以正常工作,但首先它似乎没有在修改后生成新的类文件模块 gradle 文件,就像它所说的那样。
首先,我不确定如何进行 gradle 设置。通过浏览互联网和开发者博客,我遇到了三种不同的版本:
android {
...
buildFeatures {
viewBinding = true
}
或者喜欢:
android {
...
buildFeatures {
viewBinding{
enabled = true
}
}
或者可能喜欢:
android {
...
viewBinding {
enabled = true
}
并且我已经用所有的内容构建了(或“制作”)该项目那些似乎什么也没有发生。我知道关联的布局类文件(在本例中)只是一组标准的布局和类文件,它是在使用空活动创建默认项目时附带的,应该“生成”一个类文件,其名称为其关联的布局文件,以Pascal格式编写,并在末尾添加“Binding”,因此,在本例中,布局文件称为activity_main.xml
,因此该类因此文件应该是MainActivityBinding
(我知道这是向后的,但这就是他们默认的方式:MainActivity
)
那么,在那之后,应该是模块中允许绑定的每个文件的 getroot()
方法。但它也不明白。事实上,这里提到的大部分代码都不起作用。我到处都遇到未引用的变量错误,这可能源于文件未正确生成,但我真的不知道。
我对谷歌给你的这组指示还有很多问题,但如果这是主要问题,我就从这里开始。
那么我该如何解决这个问题呢?我真正应该做什么?
更新:我通过错误发现,这就是我应该放入 gradle 文件中的内容(注意:它不会接受视图绑定而不是数据绑定)
android {
...
buildFeatures.dataBinding = true
但是新文件没有生成。我该怎么办?
activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
另外,inb4,这里是MainActivity.kt
:
package com.example.bindtestbinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
I've been following this tutorial here about how to perform view Binding, and nothing seems to work properly, but first it doesn't seem to generate the new class file after modding the module gradle file, like it says it will.
First, I'm not sure how to do the gradle setup. With my browsing of the internet and developer blogs I've come across three different versions of how to put it:
android {
...
buildFeatures {
viewBinding = true
}
or like:
android {
...
buildFeatures {
viewBinding{
enabled = true
}
}
or maybe like:
android {
...
viewBinding {
enabled = true
}
And I've built (or "made", as it were) the project with all of those and nothing seems to happen. I know that the associated layout class file, which, in this case, is just a standard set of layout and class files that comes with making a default project with an empty Activity, is supposed to "generate" a class file that's the name of its associated layout file, written in Pascal format with "Binding" tacked on the end, thus, in this case, the layout file is called activity_main.xml
, so the class file should therefore be MainActivityBinding
(which, I know is backwards, but that's how they had it by default: MainActivity
)
So then, after that, there's supposed to be a getroot()
method to every file in the module that allows binding. But it don't get that either. In fact most of the code mentioned here doesn't work. I get unreferenced variable errors all over the place, which probably stems from the file not generating properly, but I don't really know.
I've got a lot more problems with this set of directions Google gives you, but if this is the main problem, I'll just start here.
So how do I fix this? What am I really supposed to do?
UPDATE: I figured out, through and error, that this is what I'm supposed to put in the gradle file (note: it would not accept viewbinding instead of databinding)
android {
...
buildFeatures.dataBinding = true
But the new file did not generate. What should I do?
activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Also, inb4, here's MainActivity.kt
:
package com.example.bindtestbinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在 xml 中使用布局标签才能使用数据绑定和视图绑定,因此您的 xml 代码必须如下所示
更多信息
You must use layout tag in your xml for using databinding and view binding so your xml code code must be like this
more information
好吧,经过大量的试验和错误,这对我有用:
1.创建新项目,
我将我的项目命名为“BindTest”
(现在,您可能不必从头开始,但我没有冒险)
2. 添加了一个空 Activity
它生成了一个“MainActivity”类文件和一个“activity_main.xml”布局文件及其布局文件夹
编辑模块 Gradle 文件
3.在子级 的水平
Android
块,添加以下内容:buildFeatures.dataBinding = true
以下不起作用,错误建议我将其更改为上面的(这是他们在官方文档网站上建议的!):
同步 Gradle 文件
4.在
activity_bind.xml
中Exchange
androidx .constraintlayout.widget.ConstraintLayout
与简单的layout
从布局中删除以下内容(否则,您将收到一条错误消息,指出存在重复的当您在其中添加另一个布局时,其中之一):
android:layout_width="match_parent"
android:layout_height="match_parent"
删除默认的
TextView
并添加线性布局(或其他布局)(我删除了这个,因为当我尝试添加线性布局而不删除它时,设计窗口是一种方式。如果你能做到这一点,给你更多的力量,并告诉我你的黑暗秘密。)< /em>
添加一些
TextView
并给它们 IDtv1
、tv2
和tv3
5.构建
它应该生成
ActivityBindBinding
类(我很遗憾我的命名法决定),但它不重命名类文件与布局相关 文件;它只是在幕后做这件事。要实际上看看你所做的是否有效......6.编辑
BindActivity
类private Lateinit var绑定:ActivityBindBinding
onCreate
中:super
调用之后添加binding = ActivityBindBinding.inflate(layoutInflater)
setContentView
中的视图更改为binding.root
现在应该允许您直接使用 ID (
binding.tv1
)7. 在
onCreate
中测试它binding.tv1 = "Hello from inside the class"
当我在完成所有这些操作后运行应用程序时,它起作用了。
测试数组绑定
不知道你怎么想,但 Kotlin 中的数组对我来说很痛苦,所以我测试了它们以确保我做对了。
onCreate
中,删除我们创建的最后一行并调用两个方法。它应该如下所示:我再次运行应用程序:SUCCESSSSSSSSSSS!!
我想指出的是,当我尝试制作一个没有活动的项目,它无法正常工作,声称“没有设置默认活动”。因此,如果您想让自己免于头痛(因为这已经足够了),只需从空活动开始...请
Alright, so after a lot of trial and error, this is what worked for me:
1. Make New Project
I named mine "BindTest"
(Now, you may not have to start all over, but I took no chances)
2. Added an Empty Activity
It generated a "MainActivity" class file and an "activity_main.xml" layout file, along with its layout folder
3. Edit The Module Gradle File
At the child level of the
Android
block, add this:buildFeatures.dataBinding = true
The following didn't work, and the error suggested I change it to the above (And this is what they suggest on the official documentation site!):
4. Sync Gradle File
In
activity_bind.xml
:Exchange
androidx.constraintlayout.widget.ConstraintLayout
with simplylayout
Remove the following from the layout (Else, you'll get an error saying there's a duplicate of one of them when you add another layout inside):
android:layout_width="match_parent"
android:layout_height="match_parent"
Remove default
TextView
and add a Linear Layout (or some other layout)(I removed this because the Design Window was being a way when I tried to add a Linear Layout without deleting it. If you can do it, more power to you, and tell me your dark secrets.)
Throw in some
TextView
s and give them IDstv1
,tv2
, andtv3
5. Build
It should generate
ActivityBindBinding
class (I regret my nomenclature decisions), but it does NOT rename the class file associated with the layout file; it merely does it under the covers. To actually see if what you did worked...6. Edit
BindActivity
Classprivate lateinit var binding: ActivityBindBinding
onCreate
:binding = ActivityBindBinding.inflate(layoutInflater)
after thesuper
callsetContentView
to bebinding.root
You should now be allowed to use the IDs directly (
binding.tv1
)7. Test It
onCreate
, last line:binding.tv1 = "Hello from inside the class"
When I ran the app after doing all this, it worked.
Test Array Binding
Don't know about you, but arrays in Kotlin are a pain to me, so I tested them to make sure I got this right.
onCreate
, remove the last line we made and call to two methods. It should look like this:I ran the app again: SUCCESSSSSSSSSSS!!
I'd like to point out that, when I tried to make a project with No Activity, it didn't work properly, claiming that there was "no Default Activity set". So, if you want to save yourself from some headache (Because this is enough), just start with an Empty Activity...please