如何交换主页上的片段(并修复java.lang.IllegalArgumentException:找不到id为0x7f08007c的视图)
目前我在Android Studio中使用默认的导航抽屉活动结构。我在fragment_home.xml上有一个按钮。我希望按钮有一个onClickListener,当用户点击它时,用户重定向到另一个片段xml文件,在本例中,从主片段到集合片段 默认情况下,将有一个汉堡菜单供用户选择片段。但是在本例中,我希望用户也能够直接在主片段上选择片段 我在HomeFragment中有此代码(用户将看到的第一个片段页面):如何交换主页上的片段(并修复java.lang.IllegalArgumentException:找不到id为0x7f08007c的视图),java,android,Java,Android,目前我在Android Studio中使用默认的导航抽屉活动结构。我在fragment_home.xml上有一个按钮。我希望按钮有一个onClickListener,当用户点击它时,用户重定向到另一个片段xml文件,在本例中,从主片段到集合片段 默认情况下,将有一个汉堡菜单供用户选择片段。但是在本例中,我希望用户也能够直接在主片段上选择片段 我在HomeFragment中有此代码(用户将看到的第一个片段页面): R.id.mobile\u navigation是指res>navigation下的
R.id.mobile\u navigation
是指res>navigation下的mobile\u 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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.xxx.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_collection"
android:name="com.xxx.ui.collection.CollectionFragment"
android:label="@string/menu_collection"
tools:layout="@layout/fragment_collection" />
</navigation>
将目的地添加到导航图中
<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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.xxx.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">
<action
android:id="@+id/nav_home_to_nav_collection"
app:destination="@id/nav_collection" />
</fragment>
<fragment
android:id="@+id/nav_collection"
android:name="com.xxx.ui.collection.CollectionFragment"
android:label="@string/menu_collection"
tools:layout="@layout/fragment_collection" />
</navigation>
将目的地添加到导航图中
<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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.xxx.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">
<action
android:id="@+id/nav_home_to_nav_collection"
app:destination="@id/nav_collection" />
</fragment>
<fragment
android:id="@+id/nav_collection"
android:name="com.xxx.ui.collection.CollectionFragment"
android:label="@string/menu_collection"
tools:layout="@layout/fragment_collection" />
</navigation>
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Navigation.findNavController(view).navigate(R.id.nav_home_to_nav_collection);
}
});
<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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.xxx.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">
<action
android:id="@+id/nav_home_to_nav_collection"
app:destination="@id/nav_collection" />
</fragment>
<fragment
android:id="@+id/nav_collection"
android:name="com.xxx.ui.collection.CollectionFragment"
android:label="@string/menu_collection"
tools:layout="@layout/fragment_collection" />
</navigation>