-1

i am facing issue with viewbinding access child of activity toolbar inside fragment. here i am accessing edittext of toolbar inside my fragment class. but not able to access via view binding. please help me solve out this issue.

activityhome.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=".ui.activity.HomeActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_home"
        app:layout_constraintTop_toTopOf="parent"/>

    <fragment
        android:id="@+id/nav_main_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintBottom_toTopOf="@id/actHomeBottomNavigation"
        app:navGraph="@navigation/nav_graph_main"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/actHomeBottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/_4sdp"
        android:background="@color/black"
        android:visibility="visible"
        app:elevation="@dimen/_10sdp"
        app:itemIconTint="@drawable/selector_color"
        app:itemTextColor="@drawable/selector_color"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

toolbarhome.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:id="@+id/toolbarLayoutFragment"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/blackgrey">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/searchLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone">
        <ImageView
            android:id="@+id/toolbarBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_back"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
        <EditText
            android:id="@+id/toolbarEditSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="Search"
            android:textSize="@dimen/_14ssp"
            android:background="@android:color/transparent"
            android:textColorHint="@color/lightgrey"
            android:textColor="@color/white"
            app:layout_constraintStart_toEndOf="@id/toolbarBack"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>

HomeFragment.kt

    class HomeFragment : BaseFragment(), TextWatcher {
    
   lateinit var binding: FragmentHomeBinding
   lateinit var editSearch: EditText
    

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        return inflater.inflate(R.layout.fragment_home, container, false)
        binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
       
        editSearch=activity?.findViewById(R.id.toolbarEditSearch)!!
//        act?.binding?.toolbar?.toolbarEditSearch?.addTextChangedListener(this)
    }
}

error i am getting in logcat is below.

   Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
 Caused by: java.lang.NullPointerException
    at com.webforest.sft.ui.fragment.HomeFragment.onViewCreated(HomeFragment.kt:61)

1 Answers1

0

set your xml code in

<?xml version="1.0" encoding="utf-8"?>
      <layout xmlns:android="http://schemas.android.com/apk/res/android">

        <androidx.constraintlayout.widget.ConstraintLayout>

        // your other layout or anything
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </layout>

and use this code in your fagment

  private var fragmentFirstBinding: FragmentFirstBinding? = null 

in the onCreate

val binding = FragmentBlankBinding.bind(view)
    fragmentFirstBinding = binding
Pouria Hemi
  • 706
  • 5
  • 15
  • 30