android 如何修复错误-未解析的引用:布局_活动绑定?

tktrz96b  于 2023-02-06  发布在  Android
关注(0)|答案(3)|浏览(224)

在Kotlin上为课程作业创建移动应用程序。我遇到了一个问题,由于某种原因,我无法在FragmentContainerView中嵌套片段。我在互联网上找不到如何修复它。
分级编码

id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 33

    defaultConfig {
        applicationId "com.example.historicalsaratovnav"
        minSdk 28
        targetSdk 33
        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.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.0'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
    implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
    implementation 'androidx.core:core-splashscreen:1.0.0'
    implementation "androidx.fragment:fragment-ktx:1.6.0-alpha04"
}

代码布局

<androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Layout_Activity">

    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    >
        <androidx.fragment.app.FragmentContainerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="1"
                android:id="@+id/conteiner_fragment"
                app:defaultNavHost="true"
                android:gravity="center"/>
        <com.google.android.material.bottomnavigation.BottomNavigationView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:labelVisibilityMode="labeled"
                android:background="@color/white"
                app:menu="@menu/menu"
                android:id="@+id/bottom_navigation_view"
        >
        </com.google.android.material.bottomnavigation.BottomNavigationView>

    </FrameLayout>

</androidx.drawerlayout.widget.DrawerLayout>

代码文件KT

import android.content.Context
import android.os.Binder
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.AttributeSet
import android.view.View

class Layout_Activity : AppCompatActivity() {

    lateinit var binding: Layout_ActivityBinding

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

        binding.Main.setOnClickListener {

            replaceFragment(Main_App_Screen_Fragment())

        }

        binding.List.setOnClickListener {

        }

        binding.Map.setOnClickListener {

        }

        binding.Profile.setOnClickListener {

---

        }

        binding.Settings.setOnClickListener {

        }
    }

    private fun replaceFragment(fragment: Main_App_Screen_Fragment) {
        val fragmentManager = supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.conteiner_fragment, fragment)
        fragmentTransaction.commit()
    }
}

尝试将依赖项添加到Gradle,但没有任何变化。此外,当我刚刚创建绑定时,系统会自动为我生成NameBinding。之后,我将其删除并进行了自定义,但由于应用程序无法运行,我无法执行任何其他操作。

pn9klfpd

pn9klfpd1#

请在gradle中执行此操作

android {
    buildFeatures {
        viewBinding true
        dataBinding true
    }
}
50few1ms

50few1ms2#

假设您有一个名为xyz_activity.xml的布局.xml,那么android将创建一个名为XyzActivityBinding的绑定
建议您检查您.xml名称
另加

buildFeatures {
        viewBinding true
 }

在您应用程序构建器.gradle中

8oomwypt

8oomwypt3#

如果这个解决方案对你没有帮助,建议你试一次这个解决方案。你面临这个错误是因为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">

<data>

</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".settingModuleFragment.itemList.ItemsListActivity">

    <include
        android:id="@+id/toolbarItemListLayout"
        layout="@layout/toolbar" />

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_id">
    </com.google.android.gms.ads.AdView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/frame_fl"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/relative_rl">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="@dimen/_8sdp"
                android:id="@+id/rv_ll"
                android:focusableInTouchMode="true"
                android:orientation="vertical">

                <EditText
                    android:id="@+id/search_edTxt"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_35sdp"
                    android:paddingStart="@dimen/_4sdp"
                    android:background="@drawable/write_bg_color"
                    android:drawableStart="@drawable/ic_search_24"
                    android:inputType="text" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_item"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

            </LinearLayout>

            <TextView
                android:id="@+id/noItems_Tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="@dimen/_16ssp"
                android:gravity="center"
                android:visibility="gone"
                style="@style/Label_Normal"
                android:text="@string/you_haven_t_added_any_items"/>
        </FrameLayout>

        <RelativeLayout
            android:id="@+id/relative_rl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_8sdp"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="@dimen/_8sdp"
            android:layout_marginBottom="@dimen/_5sdp"
            android:layout_gravity="bottom">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/total"
                android:textSize="@dimen/_14ssp"
                android:textColor="@color/black"
                android:gravity="center"
                android:layout_alignParentStart="true"
                android:fontFamily="@font/lato_bold"/>

            <TextView
                android:id="@+id/total_item_count_Tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"

                android:fontFamily="@font/lato_bold"
                android:gravity="center"
                android:text="@string/_0_0"
                android:textColor="@color/black"
                android:textSize="@dimen/_14ssp" />

            <TextView
                android:id="@+id/total_amount_count_Tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/lato_bold"
                android:gravity="center"
                android:layout_alignParentEnd="true"

                android:text="@string/_0_0"
                android:textColor="@color/black"
                android:textSize="@dimen/_14ssp" />
  

        </RelativeLayout>

    </RelativeLayout>

  </LinearLayout>
</layout>

如果在使用DataBinding时需要使用标记,则可以使用此选项,以便编译器理解特殊标记并生成具有正确变量和方法的DataBinding类。
原答复:https://stackoverflow.com/a/44682671/19674027

相关问题