我正在尝试通过关注 this straightforward approach 将徽章设置为 BottomNavigationView
。
但是,当我初始化 BottomNavigationView 时,我得到:
java.lang.IllegalStateException: view.findViewById(R.id.bottom_navigation_view) must not be null
我正在从片段初始化 BottomNativigationView。我猜这就是问题所在,但我想不出解决方案。
private lateinit var bottomNavigation: BottomNavigationView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_home, container, false)
bottomNavigation = view.findViewById(R.id.bottom_navigation_view)
}
这是为片段设置导航的 Activity 的 BottomNavigationView xml。
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
app:itemIconTint="@color/navigation_tint"
app:itemTextColor="@color/navigation_tint"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation" />
感觉好像我错过了一些简单的东西,但我不知道是什么。谢谢!
答案 0 :(得分:1)
您有许多选项可以在片段之间进行交流 - 活动和片段本身..
您不应该尝试从片段访问活动视图。
解决方案 1:与主机活动共享数据
class ItemViewModel : ViewModel() {
private val mutableSelectedItem = MutableLiveData<Item>()
val selectedItem: LiveData<Item> get() = mutableSelectedItem
fun selectItem(item: Item) {
mutableSelectedItem.value = item
}
}
class MainActivity : AppCompatActivity() {
// Using the viewModels() Kotlin property delegate from the activity-ktx
// artifact to retrieve the ViewModel in the activity scope
private val viewModel: ItemViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.selectedItem.observe(this, Observer { item ->
// Perform an action with the latest item data
})
}
}
class ListFragment : Fragment() {
// Using the activityViewModels() Kotlin property delegate from the
// fragment-ktx artifact to retrieve the ViewModel in the activity scope
private val viewModel: ItemViewModel by activityViewModels()
// Called when the item is clicked
fun onItemClicked(item: Item) {
// Set a new item
viewModel.selectItem(item)
}
}
解决方案 2:在主机活动中接收结果
button.setOnClickListener {
val result = "result"
// Use the Kotlin extension in the fragment-ktx artifact
setFragmentResult("requestKey", bundleOf("bundleKey" to result))
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager
.setFragmentResultListener("requestKey", this) { requestKey, bundle ->
// We use a String here, but any type that can be put in a Bundle is supported
val result = bundle.getString("bundleKey")
// Do something with the result
}
}
}
还有很多方法,但这些是 Google 的最新方法。
检查此参考:https://developer.android.com/guide/fragments/communicate
答案 1 :(得分:1)
您可以通过将 activity
投射到您的活动类,然后扩充视图,从其片段访问活动。
bottomNavigation = (activity as MyActivityName).findViewById(R.id.bottom_navigation_view)