英文:
Unresolved reference: Layout_ActivityBinding?
问题
在Kotlin课程的移动应用程序上创建一个应用程序。 我遇到了一个问题,由于某种原因,我不能在FragmentContainerView中嵌套一个片段。 我在互联网上找不到如何修复它的方法。
gradle中的代码
plugins {
    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"
}
布局中的代码
<?xml version="1.0" encoding="utf-8"?>
<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>
Kotlin文件中的代码
package com.example.historicalsaratovnav
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会自动生成给我。之后,我将其删除并自定义,但由于应用程序不起作用,我无法再做其他事情。
英文:
Create Mobile App for coursework on Kotlin. I ran into a problem that for some reason I can't nest a fragment in a FragmentContainerView. I couldn't find on the internet how to fix it.
Code in gradle
    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"
}
Code layout
<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>
Code file 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()
    }
}
Tried adding dependencies to Gradle, but nothing changed. Also, when I just created the binding, NameBinding was automatically generated for me. After that, I erased it and made it custom and I can’t do anything else because the application does not work.
答案1
得分: 2
请在Gradle中执行以下操作:
android {
    buildFeatures {
        viewBinding true
        dataBinding true
    }
}
英文:
please do this in gradle
android {
    buildFeatures {
        viewBinding true
        dataBinding true
    }
}
答案2
得分: 0
以下是已翻译的内容:
假设您有一个名为xyz_activity.xml的布局文件.xml,那么Android会创建一个名为XyzActivityBinding的绑定。
建议您检查您的.xml文件名称。
还要添加以下内容到您的应用build.gradle文件中:
buildFeatures {
    viewBinding true
}
英文:
Assume that you have a layout .xml named as xyz_activity.xml
then android will create a binding named XyzActivityBinding
suggest you to check your .xml name
Also add
 buildFeatures {
        viewBinding true
 }
in your app build.gradle
答案3
得分: 0
以下是翻译的内容:
如果这个解决方案对你没有帮助,建议你尝试这个解决方案。你遇到这个错误是因为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时使用<layout>标签,以便编译器能够理解特殊标签并生成具有正确变量和方法的DataBinding类。
英文:
If this solution not helping you recommend you to try this solution once. You facing this error because of 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>
This is used if you need to use the <layout> tag whenever you are using DataBinding for the compiler to understand the special tags and generate the DataBinding class with the right variables and methods.
Original answer: https://stackoverflow.com/a/44682671/19674027
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论