在Android Studio数据绑定中,此处不允许元素数据。

8
我想制作一款类似于井字棋的安卓应用(不过是4x4网格)。我决定使用按钮来表示每个格子,并希望使用数据绑定将按钮上的文本绑定到一个String[][]数组中的数据,该数组将在内部表示网格。我尝试着做类似于这里介绍的内容:http://www.vogella.com/tutorials/AndroidDatabinding/article.html,于是我创建了这个类:
public class ModelJoc extends BaseObservable{

private String[][] tabla_joc;

public ModelJoc() {
    for (String[] line : tabla_joc)
        for (String element : line)
            element = "";

    tabla_joc[0][0] = "M";
    tabla_joc[0][1] = "W";
}

然后在activity_main.xml中添加数据绑定:

<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tgeorge.temajoc.MainActivity">
<data>
    <variable
        name="state"
        type="com.example.tgeorge.temajoc.ModelJoc"/>
</data>

然后尝试将按钮的文本设置为数组中的值:

 <Button
        android:id="@+id/button1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="@={state.getBlockState()[0][0]}"/>

但是它给我返回这些错误:"元素data在此处不允许"以及"属性缺少android前缀"。从教程示例中无法确定我的错误所在,因此问题在于我应该把它们放在哪里?

1
getBlockState()函数中包含了什么内容? - George Mount
另外,你不想在按钮中使用"@={...}"。请尝试使用"@{...}" - George Mount
@GeorgeMount 的 getBlockState() 方法返回 tabla_joc。将 "@={" 改为 "@{" 似乎没有任何效果。尝试将其绑定到一个简单的字段而不是方法也没有起作用。我认为这可能与我正在使用的 android.support.constraint.ConstraintLayout 布局有关,因为在我尝试遵循的教程和大多数其他教程中,它只是一个简单的 layout - FullStackOverflowDev
1个回答

17

我现在明白问题了。你必须使用 <layout> 标签来概述你的布局:

<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="com.example.tgeorge.temajoc.MainActivity">
    <data>
      <variable
          name="state"
          type="com.example.tgeorge.temajoc.ModelJoc"/>
    </data>
    <android.support.constraint.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- ... -->
        <Button
            android:id="@+id/button1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="@{state.getBlockState()[0][0]}"/>
如果您不这样做,Android数据绑定将无法将其识别为数据绑定的布局文件。

如果您不这样做,Android数据绑定将无法将其识别为数据绑定的布局文件。


这确实解决了我遇到的XML错误,但实际的数据绑定本身似乎并没有起作用。我绑定的按钮文本仍然为空。我还尝试使用 binding.button1.setText("m"); 以编程方式设置按钮的文本,但它仍然为空白。 - FullStackOverflowDev
1
有可能您没有使用数据绑定布局。例如,如果您使用了 DataBindingUtil.inflate() 而不是 DataBindingUtil.setContent() - George Mount
我使用了 ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); - FullStackOverflowDev
1
你是否记得将布局设置到Activity中?例如:setContentView(binding.getRoot()); 很常见的问题是在onCreate()中保留默认的setContentView(R.layout.activity_main);,而忘记将其设置为数据绑定的内容。 - George Mount
实际上,这似乎解决了使用binding.button1.setText("m");设置的文本无法工作的问题。XML绑定仍然为空,但我已经发现这是因为我没有将state分配给ModelJoc的实例。在使用binding.setState(new ModelJoc())之后,一切似乎都正常工作了。非常感谢您的帮助,这非常有启发性! - FullStackOverflowDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接