Android 导航控制器从startDestination片段导航获取异常
startDestination碎片内的MBTOME按钮 导航图xml文件Android 导航控制器从startDestination片段导航获取异常,android,navigationcontroller,android-jetpack,android-architecture-navigation,Android,Navigationcontroller,Android Jetpack,Android Architecture Navigation,startDestination碎片内的MBTOME按钮 导航图xml文件 java.lang.IllegalArgumentException: ID does not reference a View inside this Activity mBtHome.setOnClickListener(new View.OnClickListener() { @Override public void onClick(Vie
java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
mBtHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(getActivity(), R.id.homePageFragment);
}
});
导航/nav_文件添加到MainActivity xml文件中NavHostFragment也包括在内,还添加了defaultNavHost=“true”根据上述代码,单击按钮可从一个片段导航到另一个片段,有多种方法 导航类为您提供了createNavigationClickListner()方法,您只需要传递片段操作id 请尝试下面的代码
<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/nav_file"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.example.libin.navigationhelphertest.ui.main.MainFragment"
android:label="main_fragment"
tools:layout="@layout/main_fragment">
<action
android:id="@+id/action_mainFragment_to_homePageFragment"
app:destination="@id/homePageFragment" />
<action
android:id="@+id/action_mainFragment_to_usersListFragment"
app:destination="@id/usersListFragment" />
</fragment>
<fragment
android:id="@+id/homePageFragment"
android:name="com.example.libin.navigationhelphertest.ui.main.HomePageFragment"
android:label="fragment_home_page"
tools:layout="@layout/fragment_home_page" />
<fragment
android:id="@+id/usersListFragment"
android:name="com.example.libin.navigationhelphertest.ui.main.UsersListFragment"
android:label="fragment_users_list"
tools:layout="@layout/fragment_users_list" />
</navigation>
R.id.homePageFragment不是NavHostFragment 像这样做吗
mBtHome.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_mainFragment_to_homePageFragment));
你能发布你的导航图xml文件吗?@Android_team从你的主片段中用xmlSo编辑你正在做这段代码并导航到Home Fragemt right?mBtHome.setOnClickListener(new View.OnClickListener(){@Override public void onClick(视图视图){Navigation.findNavController(getActivity(),R.id.homePageFragment);})就这样,我有两个actions@LibinThomas你得到答案了吗?我从未见过任何示例使用
CreateNavigationClickListener
。这在JetPack中有记录吗?是的,它记录在其开发人员的网站中用于导航。请查看下面的链接,为什么没有在任何Google示例或任何示例中演示?不,它在cod中使用elab代码。请参见下面的链接Sunflower示例、Tivi示例、ioSched示例和Plaid google示例均未演示此API。为什么?为什么此API优于NavGraph方向API?
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container_view"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/motion_layout_graph" />
Navigation.findNavController(requireActivity(), R.id.fragment_container_view).navigate(R.id.show_one)