英文:
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:
<?xml version="1.0" encoding="utf-8"?>
<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>
The Navgraph:
<?xml version="1.0" encoding="utf-8"?>
<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>
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("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");
// pass the data to the fragment using the fragment's arguments
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.firstPatientFragment);
Log.d("PatientActivity", "Fragment found: " + fragment);
if (fragment != null) {
fragment.getArguments().putAll(bundle);
}
}
FirstFragment in Patient Activity:
Bundle bundle = getArguments();
Log.d("MyFragment", "Bundle received: " + bundle);
if (bundle != null) {
String value = bundle.getString("specialities");
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论