我找不到在navHostFragment中的碎片。

huangapple go评论61阅读模式
英文:

I can't find the fragment in the navHostFragment

问题

Patient activity有3个fragments,这些fragments位于一个fragmentcontainerview中,带有导航图(navgraph),还有一个Speciality activity,专门用于显示专业列表。

你想要从Speciality activity发送一个意图(intent)到主要活动的第一个fragment。

以下是你提供的XML代码:

Patient XML:

<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"
    tools:context=".PatientHome">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/patient_bottom_menu"
        app:itemTextColor="@color/bottom_nav_color"
        app:itemIconTint="@color/bottom_nav_color"/>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/patientfragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        android:tag="customTag"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/patient_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>

Navgraph:

<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/patient_nav"
    app:startDestination="@id/firstPatientFragment">

    <fragment
        android:id="@+id/firstPatientFragment"
        android:name="com.topaholic.hptracker.FirstPatientFragment"
        android:label="fragment_first_patient"
        tools:layout="@layout/fragment_first_patient" />
    <fragment
        android:id="@+id/secondPatientFragment"
        android:name="com.topaholic.hptracker.SecondPatientFragment"
        android:label="fragment_second_patient"
        tools:layout="@layout/fragment_second_patient" />
    <fragment
        android:id="@+id/thirdPatientFragment"
        android:name="com.topaholic.hptracker.ThirdPatientFragment"
        android:label="fragment_third_patient"
        tools:layout="@layout/fragment_third_patient" />
</navigation>

你想要从Speciality activity发送一个意图到Patient activity的第一个fragment。

Speciality Activity:

Intent intent = new Intent(SpecialityActivity.this, PatientHome.class);
Bundle bundle = new Bundle();
bundle.putString("specialities", getResources().getStringArray(R.array.specialities)[i]);
intent.putExtras(bundle);
Log.d("Speciality", "Bundle sent: " + bundle);
startActivity(intent);

Patient Activity:

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    String value = bundle.getString("specialities");
    // 使用fragment的参数传递数据
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.firstPatientFragment);
    Log.d("PatientActivity", "Fragment found: " + fragment);
    if (fragment != null) {
        fragment.getArguments().putAll(bundle);
    }
}

Patient Activity的第一个Fragment:

Bundle bundle = getArguments();
Log.d("MyFragment", "Bundle received: " + bundle);
if (bundle != null) {
    String value = bundle.getString("specialities");
    // 在此设置你的UI元素
    specbtn.setText(value);
}

从你的日志来看,Patient Activity中的fragment似乎为null。请确保fragment的ID与XML布局文件中定义的ID匹配,以及确保导航图正确配置。如果问题仍然存在,你可能需要检查导航图和fragment的关联以确保数据正确传递。

英文:

I have a Patient activity with 3 fragments in a fragmentcontainerview with navgraph and I have Speciality activity that is about a listview of specialities
I want to send and intent from the speciality activity to the first fragment in the main activity

Patient XML:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.PatientHome&quot;&gt;

&lt;com.google.android.material.bottomnavigation.BottomNavigationView
    android:id=&quot;@+id/bottomNavigationView&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:background=&quot;#fff&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:menu=&quot;@menu/patient_bottom_menu&quot;
    app:itemTextColor=&quot;@color/bottom_nav_color&quot;
    app:itemIconTint=&quot;@color/bottom_nav_color&quot;/&gt;

&lt;androidx.fragment.app.FragmentContainerView
    android:id=&quot;@+id/patientfragmentContainerView&quot;
    android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    app:defaultNavHost=&quot;true&quot;
    android:tag=&quot;customTag&quot;
    app:layout_constraintBottom_toTopOf=&quot;@+id/bottomNavigationView&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;
    app:navGraph=&quot;@navigation/patient_nav&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

The Navgraph:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/patient_nav&quot;
app:startDestination=&quot;@id/firstPatientFragment&quot;&gt;

&lt;fragment
    android:id=&quot;@+id/firstPatientFragment&quot;
    android:name=&quot;com.topaholic.hptracker.FirstPatientFragment&quot;
    android:label=&quot;fragment_first_patient&quot;
    tools:layout=&quot;@layout/fragment_first_patient&quot; /&gt;
&lt;fragment
    android:id=&quot;@+id/secondPatientFragment&quot;
    android:name=&quot;com.topaholic.hptracker.SecondPatientFragment&quot;
    android:label=&quot;fragment_second_patient&quot;
    tools:layout=&quot;@layout/fragment_second_patient&quot; /&gt;
&lt;fragment
    android:id=&quot;@+id/thirdPatientFragment&quot;
    android:name=&quot;com.topaholic.hptracker.ThirdPatientFragment&quot;
    android:label=&quot;fragment_third_patient&quot;
    tools:layout=&quot;@layout/fragment_third_patient&quot; /&gt;
&lt;/navigation&gt;

So from the activity speciality I'm trying to intent to Patient activity then to the fragment activity

Speciality Activity:

Intent intent = new Intent(SpecialityActivity.this, PatientHome.class);
            Bundle bundle = new Bundle();
            bundle.putString(&quot;specialities&quot;, 
            getResources().getStringArray(R.array.specialities)[i]);
            intent.putExtras(bundle);
            Log.d(&quot;Speciality&quot;, &quot;Bundle sent: &quot; + bundle);
            startActivity(intent);

Patient Activity:

Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String value = bundle.getString(&quot;specialities&quot;);
        // pass the data to the fragment using the fragment&#39;s arguments
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.firstPatientFragment);
        Log.d(&quot;PatientActivity&quot;, &quot;Fragment found: &quot; + fragment);
        if (fragment != null) {
            fragment.getArguments().putAll(bundle);
        }
    }

FirstFragment in Patient Activity:

Bundle bundle = getArguments();
    Log.d(&quot;MyFragment&quot;, &quot;Bundle received: &quot; + bundle);
    if (bundle != null) {
        String value = bundle.getString(&quot;specialities&quot;);
        specbtn.setText(value);
    }

Here are the Logs I get:
Speciality Activity: Bundle sent: Bundle[{specialities=Diagnostic Radiology}]
Patient Activity: Fragment found: null
FirstFragment: Bundle received: null

So idk it is giving that fragment in null in the FragmentContainerView

答案1

得分: 1

你忘记在访问患者活动中的片段时使用导航图来初始化FragmantContainerView。

英文:

You forgot to initialize FragmantContainerView with NavGraph when accessing the fragment in patient activity.

huangapple
  • 本文由 发表于 2023年2月27日 04:36:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定