我正在使用Fragments和Android studio,我需要在Activity中的片段中进行导航。总是出现相同的错误“找不到ID为0x7f0800a2的视图”。
主要活动
Fragment newFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_hotels, newFragment);
transaction.addToBackStack(null);
transaction.commit();
mobile_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.example.freepapp.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">
</fragment>
<fragment
android:id="@+id/nav_hotels"
android:name="com.example.freepapp.ui.hotels.HotelsFragment"
android:label="@string/menu_hotels"
tools:layout="@layout/activity_liked_quotes" />
Fragment Home
<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"
android:id="@+id/fragment_home"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
[碎片酒店
<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"
android:id="@+id/fragment_hotels"
tools:context=".ui.hotels.HotelsFragment">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
错误
java.lang.IllegalArgumentException:找不到片段HomeFragment {f896362(40055ce2-7ab7-41dd-be0b-be0edc89f01a)id = 0x7f0800a2}的ID 0x7f0800a2(com.example.freepapp:id / nav_hotels)的视图
我正在使用Fragments和Android studio,我需要在Activity中的片段中进行导航。总是出现相同的错误“找不到ID为0x7f0800a2的视图”。主要活动...
我找到了答案。容器是布局主要活动的ID。