简体   繁体   English

Android kotlin ViewBinding kotlin.UninitializedPropertyAccessException: lateinit 属性绑定尚未初始化

[英]Android kotlin ViewBinding kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized

So I want to access a view in a fragment from another fragment.所以我想从另一个片段访问一个片段中的视图。 Here I use viewBinding in both fragment.在这里,我在两个片段中都使用了 viewBinding。 This is what the layout looks like where the View I want to access from other fragment is:这是布局的样子,我想从其他片段访问的视图是:

fragment_dashboard.xml fragment_dashboard.xml

  <layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/content"/>

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:id="@+id/bottomNavigationView"
                android:background="?android:attr/windowBackground"
                app:itemBackground="@color/buttonColor"
                app:itemIconTint="@drawable/bottom_navigation_selector"
                app:itemTextColor="@drawable/bottom_navigation_selector"
                app:menu="@menu/bottom_navigation_menu"/>


        </LinearLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:itemTextColor="@color/buyItemButtonColor"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/nav_drawer_menu" />


    </androidx.drawerlayout.widget.DrawerLayout>

</layout>

This is what fragment_dashboard.xml 's Fragment snippet code:这是fragment_dashboard.xml的片段代码:

DashboardFragment.kt DashboardFragment.kt

class DashboardFragment : Fragment(), NavigationView.OnNavigationItemSelectedListener {

lateinit var binding: FragmentDashboardBinding
var currentSelectedMenu by Delegates.notNull<Int>()

lateinit var username: String
lateinit var memberId: String

private val onNavigateItemListener = BottomNavigationView.OnNavigationItemSelectedListener{ item ->
    ...
}


private fun addFragment(fragment: Fragment) {
    fragmentManager
        ?.beginTransaction()
        ?.setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
        ?.replace(R.id.content, fragment, fragment.javaClass.simpleName)
        ?.commit()
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    binding = DataBindingUtil.inflate(
        inflater,
        R.layout.fragment_dashboard, container, false
    )

    val args = arguments?.let {
        DashboardFragmentArgs.fromBundle(
            it
        )
    }


    if (args != null) {
        binding.navView.setNavigationItemSelectedListener(this)
    }

    binding.bottomNavigationView.setOnNavigationItemSelectedListener(onNavigateItemListener)

    val fragment = HomeFragment.newInstance(memberId, username)
    addFragment(fragment)

    // Inflate the layout for this fragment
    return binding.root
}


fun openNavDrawer(){
    binding.drawerLayout.openDrawer(GravityCompat.START)
}

The function openNavDrawer() will be executed from another fragment ( HomeFragment.kt ). function openNavDrawer()将从另一个片段 ( HomeFragment.kt ) 执行。 Here's the snippet code from HomeFragment.kt这是来自HomeFragment.kt的代码片段

HomeFragment.kt HomeFragment.kt

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)

    ...

    binding.navigationDrawerTrigger.setOnClickListener {
        DashboardFragment().openNavDrawer()
    }

}

But when executed, it returned:但是当执行时,它返回:

kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
    at com.example.centuryememberproject.dashboard.DashboardFragment.openNavDrawer(DashboardFragment.kt:154)
    at com.example.bottomnavigationexample.HomeFragment$onCreateView$1.onClick(HomeFragment.kt:83)
    at android.view.View.performClick(View.java:6719)
    at android.view.View.performClickInternal(View.java:6677)
    at android.view.View.access$3400(View.java:797)
    at android.view.View$PerformClick.run(View.java:26475)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:226)
    at android.app.ActivityThread.main(ActivityThread.java:7212)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:956)

Did I miss something?我错过了什么? Or maybe the variable binding from in the DashboardFragment.kt should moved elsewhere?或者DashboardFragment.kt中的变量binding应该移到别处? If there's any detail I miss to point out, just let me know !如果有任何细节我想指出,请告诉我!

  • Caused引起的

When You click in HomeFragment with DashboardFragment().openNavDrawer() ,当您使用DashboardFragment().openNavDrawer()单击HomeFragment时,

binding.navigationDrawerTrigger.setOnClickListener {
        DashboardFragment().openNavDrawer()
    }

it means you call openNavDrawer() with a new fragment instance of DashboardFragment , (the new one)这意味着您使用DashboardFragmentnew fragment instance (新的)调用openNavDrawer() )

not the original fragment .不是the original fragment (the original one) (原来的)


  • Why为什么

it has not been initialized?它没有被初始化?

Because it (the new one) did not managed by your FragmentManager and show on screen so that will not do createView()因为它(新的)没有由您的 FragmentManager 管理并显示在屏幕上,所以不会执行 createView()

(although you should not do this with DashboardFragment()) (虽然你不应该用 DashboardFragment() 这样做)


  • What Should you do你该怎么办

You should use the original dashboard fragment to do what you want to do.你应该使用the original dashboard fragment来做你想做的事。

Such as:如:

  1. Pass dashboard reference into homedashboard reference传递到home
  2. find the dashboard fragment by your fragmentManager in home通过您的 fragmentManager home中找到dashboard fragment

then home can interact with dashboard然后home可以与dashboard交互

binding.navigationDrawerTrigger.setOnClickListener {
        the_origin_dashboard_fragment.openNavDrawer()
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 kotlin.UninitializedPropertyAccessException:lateinit 属性 roomClickedInterface 尚未初始化 - kotlin.UninitializedPropertyAccessException: lateinit property roomClickedInterface has not been initialized kotlin.UninitializedPropertyAccessException:lateinit 属性 adapterPosts 尚未初始化 - kotlin.UninitializedPropertyAccessException: lateinit property adapterPosts has not been initialized kotlin.UninitializedPropertyAccessException: lateinit 属性 backendApi 尚未在 UniTest 中初始化? - kotlin.UninitializedPropertyAccessException: lateinit property backendApi has not been initialized in UniTest? 原因:kotlin.UninitializedPropertyAccessException:lateinit属性idLeague尚未初始化 - Caused by: kotlin.UninitializedPropertyAccessException: lateinit property idLeague has not been initialized kotlin.UninitializedPropertyAccessException:lateinit 属性 layoutManager 尚未初始化 - kotlin.UninitializedPropertyAccessException: lateinit property layoutManager has not been initialized :kotlin.UninitializedPropertyAccessException:lateinit 属性管理器尚未初始化 - : kotlin.UninitializedPropertyAccessException: lateinit property manager has not been initialized lateinit 属性 apiInterface 尚未初始化 kotlin.UninitializedPropertyAccessException:? - lateinit property apiInterface has not been initialized kotlin.UninitializedPropertyAccessException:? kotlin.UninitializedPropertyAccessException:lateinit属性mRepository尚未初始化 - kotlin.UninitializedPropertyAccessException: lateinit property mRepository has not been initialized kotlin.UninitializedPropertyAccessException:lateinit 属性 WebView 尚未初始化 - kotlin.UninitializedPropertyAccessException: lateinit property WebView has not been initialized 原因:kotlin.UninitializedPropertyAccessException:lateinit 属性对话框尚未初始化 - Caused by: kotlin.UninitializedPropertyAccessException: lateinit property dialog has not been initialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM