在我的Fragment类中,我有这个:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.my_menu, menu)
val searchItem = menu.findItem(R.id.action_search)
searchView = searchItem.actionView as SearchView
searchView.apply{
queryHint = "Search"
isIconified = false
setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String): Boolean {
// pass the query to the other fragment and navigate to it
findNavController().navigate(GroupListFragmentDirections.actionGroupListFragmentToSearchedGroupListFragment(query))
return false
}
override fun onQueryTextChange(newText: String): Boolean {
// start the search
//groupListViewModel.searchGroups(newText)
return false
}
})
}
}
现在,发生的事情是我想将查询传递给另一个片段并导航到它。然后,另一个片段以某种方式处理查询。但是Android Studio告诉我SearchView没有设置NavController。这是我在LogCat中找到的错误消息:
java.lang.IllegalStateException: View androidx.appcompat.widget.SearchView{c83b4ef VFE...... ........ 147,10-987,136 #7f08003d app:id/action_search} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.java:84)
at androidx.navigation.ViewKt.findNavController(View.kt:28)
at com.celik.abdullah.mylim4.ui.GroupListFragment$onCreateOptionsMenu$1$1.onQueryTextSubmit(GroupListFragment.kt:93)
at androidx.appcompat.widget.SearchView.onSubmitQuery(SearchView.java:1191)
at androidx.appcompat.widget.SearchView$7.onEditorAction(SearchView.java:1168)
at android.widget.TextView.onEditorAction(TextView.java:5911)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:360)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
...
我使用Android Architecture Components中的NavigationUI库。 MainActivity的布局包含NavHostFragment布局小部件。您在上面看到的片段代码是起始片段(用户在打开应用程序时看到的片段)。
我假定不允许在searchview的侦听器内使用findNavController()。不过,是否有办法将查询从搜索视图传递到下一个片段。
这是我的活动布局:
// activity_main.xml
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment
android:id="@+id/myNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
</LinearLayout>
和这里是我的navigation.xml:
<navigation 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:id="@+id/navigation"
app:startDestination="@id/groupListFragment">
<fragment
android:id="@+id/groupListFragment"
android:name="com.celik.abdullah.mylim4.ui.GroupListFragment"
android:label="GroupListFragment"
tools:layout="@layout/fragment_group_list">
<action
android:id="@+id/action_groupListFragment_to_searchedGroupListFragment"
app:destination="@id/searchedGroupListFragment" />
</fragment>
<fragment
android:id="@+id/searchedGroupListFragment"
android:name="com.celik.abdullah.mylim4.ui.SearchedGroupListFragment"
android:label="SearchedGroupListFragment"
tools:layout="@layout/fragment_searched_group_list">
<argument
android:name="query"
app:argType="string" />
</fragment>
</navigation>
如果您在SearchView
中使用Toolbar
,则应在导航中设置Toolbar
: