【问题标题】:Today I try to viewBinding but end up with error今天我尝试查看绑定但最终出现错误
【发布时间】:2022-01-24 23:24:46
【问题描述】:

我尝试从我的 xml 文件中查看绑定,但最终出现错误。我完全从 youtube 复制,但仍然错误。 ActivityMainBinding 都是错误的。

这里是我的xml文件代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的 kotlin 代码:

package com.haziqharis.myapplicationtest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}

这是我的 gradle 代码

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.haziqharis.myapplicationtest"
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    dataBinding {
        enabled true
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

错误如下图

【问题讨论】:

    标签: android android-studio kotlin android-viewbinding


    【解决方案1】:

    您应该将以下代码放入您的 ConstraintLayout 之外的 activity_main.xml 文件中。

    <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">
    
    </layout>
    

    【讨论】:

    • @HaziqHaris 如果对您有帮助,您可以使用问题左上角的复选标记接受它!
    • 欢迎您,@HaziqHaris
    • 这里混淆了dataBinding和ViewBinding,对于viewBinding,你的xml中不需要这个layout标签,你只需要在build.gradle(app level)中开启viewBinding,然后初始化viewbinding在活动中并在 setContentView developer.android.com/topic/libraries/view-binding 中传递它的根
    【解决方案2】:

    如果您想尝试视图绑定,请将其添加到您的 gradle (build.gradle(:app))

    buildFeatures {
        viewBinding true
    }
    

    【讨论】:

      【解决方案3】:

      我对我的 xml 文件进行了一些更改。我特意将ConstraintLayout 元素做成了一个更全局的布局。

      这是我的最终代码:

      <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">
      
      
          <androidx.constraintlayout.widget.ConstraintLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".MainActivity">
      
              <Button
                  android:id="@+id/button"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Button"
                  app:layout_constraintBottom_toBottomOf="parent"
                  app:layout_constraintEnd_toEndOf="parent"
                  app:layout_constraintStart_toStartOf="parent"
                  app:layout_constraintTop_toTopOf="parent" />
      
          </androidx.constraintlayout.widget.ConstraintLayout>
      </layout>
      

      【讨论】:

        【解决方案4】:

        您应该从 Android 开发者那里结帐 this article

        在你的实现中有一些明显错误的事情你的视图应该有&lt;layout&gt;标签然后在你的onCreate()方法中你应该使用ActivityMainBinding.setContentView()而不需要调用常规的setContentView()方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-08
          • 2015-04-03
          • 2021-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多