【问题标题】:navigation between fragments in JavaJava中的片段之间的导航
【发布时间】:2020-04-16 10:50:39
【问题描述】:

我正在使用 Fragments 和 Android 工作室,我需要在活动的片段中导航。我总是收到相同的错误“找不到 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" />

片段主页

<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) 的视图

【问题讨论】:

    标签: java android-fragments navigation uinavigationcontroller fragment


    【解决方案1】:

    我找到了答案。容器是布局主要活动的 id。

    transaction.replace(R.id.id_layout_main, newFragment);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      相关资源
      最近更新 更多