1

我正在学习使用 Kotlin 进行 Android 开发,但遇到了一个我无法解决的问题。

运行构建时出现以下错误:

Code\DiceRoller\app\src\main\java\com\example\diceroller\MainActivity.kt: (16, 34): Unresolved reference: rollButto

这是我的 MainActivity.kt 代码:

package com.example.diceroller

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import com.example.diceroller.databinding.DiceRollerMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: DiceRollerMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DiceRollerMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        val rollButton = binding.rollButton
//        this.binding.rollButton.setOnClickListener {
//            Toast.makeText(this, "Button Clicked!", Toast.LENGTH_SHORT).show()
//        }
    }
}

一切正常,直到尝试访问会产生参考错误的滚动按钮,即使它出现在自动完成中。我也尝试访问另一个元素,它会产生相同的错误。

这是我的 xml 文件(dice_roller_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andriod="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.andriod.com/tools"
    andriod:layout_height="wrap_content"
    andriod:layout_width="match_parent"
    andriod:orientation="vertical"
    tools:context=".MainActivity"
    andriod:layout_gravity="center_vertical">

    <TextView
        andriod:id="@+id/name"
        andriod:layout_width="wrap_content"
        andriod:layout_height="wrap_content"
        andriod:text="1"
        andriod:layout_gravity="center_horizontal"
        andriod:textSize="30sp"/>
    
    <Button
        andriod:id="@+id/roll_button"
        andriod:layout_width="wrap_content"
        andriod:layout_height="wrap_content"
        andriod:text="@string/ButtonRoll"
        andriod:layout_gravity="center_horizontal"/>

</LinearLayout>

我确保在 gradle build 中打开视图绑定

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.example.diceroller"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

有人遇到过这个问题吗?我试过在这里和谷歌上搜索这个问题,但找不到和我有类似问题的人。

编辑:

我已经完成了生成的绑定类,看起来在 xml 中定义的按钮和文本视图没有生成代码

// Generated by view binder compiler. Do not edit!
package com.example.diceroller.databinding;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;
import com.example.diceroller.R;
import java.lang.NullPointerException;
import java.lang.Override;

public final class DiceRollerMainBinding implements ViewBinding {
  @NonNull
  private final LinearLayout rootView;

  private DiceRollerMainBinding(@NonNull LinearLayout rootView) {
    this.rootView = rootView;
  }

  @Override
  @NonNull
  public LinearLayout getRoot() {
    return rootView;
  }

  @NonNull
  public static DiceRollerMainBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static DiceRollerMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.dice_roller_main, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static DiceRollerMainBinding bind(@NonNull View rootView) {
    if (rootView == null) {
      throw new NullPointerException("rootView");
    }

    return new DiceRollerMainBinding((LinearLayout) rootView);
  }
}
4

1 回答 1

0

可能问题出在您的 xml 文件上。对于数据绑定,您必须将元素包装在组件内layout。您的 xml 文件应如下所示:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.andriod.com/tools">
    <LinearLayout 
        andriod:layout_height="wrap_content"
        andriod:layout_width="match_parent"
        andriod:orientation="vertical"
        tools:context=".MainActivity"
        andriod:layout_gravity="center_vertical">
    
        <TextView
            andriod:id="@+id/name"
            andriod:layout_width="wrap_content"
            andriod:layout_height="wrap_content"
            andriod:text="1"
            andriod:layout_gravity="center_horizontal"
            andriod:textSize="30sp"/>
    
        <Button
            andriod:id="@+id/roll_button"
            andriod:layout_width="wrap_content"
            andriod:layout_height="wrap_content"
            andriod:text="@string/ButtonRoll"
            andriod:layout_gravity="center_horizontal"/>
    
    </LinearLayout>
</layout>
于 2020-08-08T10:13:42.270 回答