Unresolved reference: Layout_ActivityBinding?

huangapple go评论46阅读模式
英文:

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 &#39;com.android.application&#39;
    id &#39;org.jetbrains.kotlin.android&#39;
}

android {
    compileSdk 33

    defaultConfig {
        applicationId &quot;com.example.historicalsaratovnav&quot;
        minSdk 28
        targetSdk 33
        versionCode 1
        versionName &quot;1.0&quot;

        testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
    }

    dataBinding{enabled=true}

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

dependencies {

    implementation &#39;androidx.core:core-ktx:1.9.0&#39;
    implementation &#39;androidx.appcompat:appcompat:1.6.0&#39;
    implementation &#39;com.google.android.material:material:1.8.0&#39;
    implementation &#39;androidx.constraintlayout:constraintlayout:2.1.4&#39;
    testImplementation &#39;junit:junit:4.13.2&#39;
    androidTestImplementation &#39;androidx.test.ext:junit:1.1.5&#39;
    androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.5.1&#39;
    implementation &#39;com.google.android.material:material:1.8.0&#39;
    implementation(&quot;androidx.navigation:navigation-fragment-ktx:2.5.3&quot;)
    implementation(&quot;androidx.navigation:navigation-ui-ktx:2.5.3&quot;)
    implementation &#39;androidx.core:core-splashscreen:1.0.0&#39;
    implementation &quot;androidx.fragment:fragment-ktx:1.6.0-alpha04&quot;
}

Code layout

&lt;androidx.drawerlayout.widget.DrawerLayout
        xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:tools=&quot;http://schemas.android.com/tools&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        tools:context=&quot;.Layout_Activity&quot;&gt;

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

    &lt;/FrameLayout&gt;

&lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
&lt;data&gt;
&lt;/data&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.settingModuleFragment.itemList.ItemsListActivity&quot;&gt;
&lt;include
android:id=&quot;@+id/toolbarItemListLayout&quot;
layout=&quot;@layout/toolbar&quot; /&gt;
&lt;com.google.android.gms.ads.AdView
xmlns:ads=&quot;http://schemas.android.com/apk/res-auto&quot;
android:id=&quot;@+id/adView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_horizontal&quot;
ads:adSize=&quot;BANNER&quot;
ads:adUnitId=&quot;@string/banner_ad_id&quot;&gt;
&lt;/com.google.android.gms.ads.AdView&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;FrameLayout
android:id=&quot;@+id/frame_fl&quot;
android:layout_alignParentTop=&quot;true&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_above=&quot;@id/relative_rl&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_margin=&quot;@dimen/_8sdp&quot;
android:id=&quot;@+id/rv_ll&quot;
android:focusableInTouchMode=&quot;true&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;EditText
android:id=&quot;@+id/search_edTxt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;@dimen/_35sdp&quot;
android:paddingStart=&quot;@dimen/_4sdp&quot;
android:background=&quot;@drawable/write_bg_color&quot;
android:drawableStart=&quot;@drawable/ic_search_24&quot;
android:inputType=&quot;text&quot; /&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/rv_item&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
app:layoutManager=&quot;androidx.recyclerview.widget.LinearLayoutManager&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;TextView
android:id=&quot;@+id/noItems_Tv&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:textSize=&quot;@dimen/_16ssp&quot;
android:gravity=&quot;center&quot;
android:visibility=&quot;gone&quot;
style=&quot;@style/Label_Normal&quot;
android:text=&quot;@string/you_haven_t_added_any_items&quot;/&gt;
&lt;/FrameLayout&gt;
&lt;RelativeLayout
android:id=&quot;@+id/relative_rl&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;@dimen/_8sdp&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:layout_marginEnd=&quot;@dimen/_8sdp&quot;
android:layout_marginBottom=&quot;@dimen/_5sdp&quot;
android:layout_gravity=&quot;bottom&quot;&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;@string/total&quot;
android:textSize=&quot;@dimen/_14ssp&quot;
android:textColor=&quot;@color/black&quot;
android:gravity=&quot;center&quot;
android:layout_alignParentStart=&quot;true&quot;
android:fontFamily=&quot;@font/lato_bold&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/total_item_count_Tv&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerInParent=&quot;true&quot;
android:fontFamily=&quot;@font/lato_bold&quot;
android:gravity=&quot;center&quot;
android:text=&quot;@string/_0_0&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;@dimen/_14ssp&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/total_amount_count_Tv&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:fontFamily=&quot;@font/lato_bold&quot;
android:gravity=&quot;center&quot;
android:layout_alignParentEnd=&quot;true&quot;
android:text=&quot;@string/_0_0&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;@dimen/_14ssp&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;/RelativeLayout&gt;
&lt;/LinearLayout&gt;
&lt;/layout&gt;

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

huangapple
  • 本文由 发表于 2023年2月6日 20:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定