【问题标题】:Not able to access toolbar in fragment viewbindig无法访问片段视图绑定中的工具栏
【发布时间】:2021-05-18 08:47:38
【问题描述】:

我面临片段内活动工具栏的视图绑定访问子项的问题。 在这里,我正在访问片段类中工具栏的编辑文本。但无法通过视图绑定访问。请帮我解决这个问题。

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)
    }
}

我在 logcat 中遇到的错误如下。

   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)

【问题讨论】:

  • 问题在于膨胀片段而不是工具栏
  • 感谢@MohitAjwani 的回复。如果我删除 editSearch=activity?... 行然后它的作品
  • 我已经添加了片段类的完整代码
  • toolbarhome.xml 在哪里使用?您的片段使用fragment_home.xml?
  • 你好 @HenryTwist ,在 homeactivity.xml 中。它没有使用。但我必须在片段中访问它的孩子。

标签: android kotlin android-viewbinding


【解决方案1】:

设置您的xml 代码

<?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>

并在你的 fagment 中使用此代码

  private var fragmentFirstBinding: FragmentFirstBinding? = null 

onCreate

val binding = FragmentBlankBinding.bind(view)
    fragmentFirstBinding = binding

【讨论】:

  • toolbarhome.xml 在 activity_home 的 inlcude 布局内。所以它不能从家庭片段访问
  • 类似的xml片段中设置工具栏布局
  • 那是不可能的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多