Estaba creando una prueba de concepto con android studio, y he intentado crear una transición entre 2 fragmentos pero me falla. El código que hice para hacer esta transición es el siguiente:
public class PartnersFragment extends Fragment {
PartnerController controller = new PartnerController();
private TableLayout partnersView;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.partners, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.partners_add_new).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(PartnersFragment.this)
.navigate(R.id.action_AddPartnerFragment); //this line throws the error
}
});
partnersView = view.findViewById(R.id.partners_table);
}
}
Pero en la línea .navigate(); me lanza el siguiente error:
Navigation action/destination administration.MyProject/action_AddPartnerFragment cannot be found from the current destination Destination(administration.MyProject/addPartnerFragment) label=partnersFragmentLabel class=administration.MyProject.AddPartnerFragment
El código de nav_graph.xml es el siguiente:
<?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/nav_graph"
app:startDestination="@id/FirstFragment">
<fragment
android:id="@+id/FirstFragment"
android:name="administracion.bigsur.HomeFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/home">
<action
android:id="@+id/action_FirstFragment_to_SecondFragment"
app:destination="@id/SecondFragment" />
</fragment>
<fragment
android:id="@+id/SecondFragment"
android:name="administracion.bigsur.PartnersFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/partners">
<action
android:id="@+id/action_AddPartnerFragment"
app:destination="@id/addPartnerFragment" />
</fragment>
<fragment
android:id="@+id/addPartnerFragment"
android:name="administracion.bigsur.AddPartnerFragment"
android:label="@string/add_partners_fragment_label"
tools:layout="@layout/add_partner" >
<action
android:id="@+id/action_addPartnerFragment_to_SecondFragment"
app:destination="@id/SecondFragment" />
</fragment>
</navigation>
Se ve de la siguiente forma:
La primera transición (FirstFragment a SecondFragment fue OK, pero la siguiente no :(, ay no se por qué si ambas las declaré de la misma forma)
El código de AddPartnerFragment es el siguiente:
public class AddPartnerFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.add_partner, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.cancel_button).setOnClickListener(v -> NavHostFragment.findNavController(AddPartnerFragment.this)
.navigate(R.id.action_partners));
}
}
el código de partners.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=".PartnersFragment"
app:navGraph="@navigation/nav_graph" >
<TableLayout
android:id="@+id/partners_table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="end" >
<TableRow android:gravity="center"><Button
android:id="@+id/partners_add_new"
android:layout_width="wrap_content"
android:gravity="end"
android:text="@string/partners_add_new"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.Button.Colored"/>
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
y el de add_partner.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=".AddPartnerFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ScrollNewPartner"
android:scrollbars="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow android:gravity="center">
<TextView android:layout_gravity="center"
android:text="@string/add_new_partner"
style="@style/Widget.AppCompat.Light.ActionBar.TabText"
/>
</TableRow>
<EditText
android:id="@+id/PartnerName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:hint="@string/partner_name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/PartnerLastName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:hint="@string/partner_lastname"
android:inputType="textPersonName"
tools:layout_editor_absoluteY="48dp" />
<EditText
android:id="@+id/PartnerPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:hint="@string/partner_phone"
android:inputType="phone"
tools:layout_editor_absoluteY="48dp" />
<EditText
android:id="@+id/PartnerEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:hint="@string/partner_email"
android:inputType="textEmailAddress"
tools:layout_editor_absoluteY="48dp" />
<TableRow android:gravity="center">
<Button
android:id="@+id/partners_save_partner"
android:layout_width="wrap_content"
android:gravity="center"
android:text="@string/partners_save_partner"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.Button.Colored"/>
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/cancel_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="@style/Widget.AppCompat.Button.Colored"/>
</TableRow>
</TableLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Código del diseño del primer fragmento (home.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=".HomeFragment">
<TextView
android:id="@+id/textview_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_first_fragment"
app:layout_constraintBottom_toTopOf="@id/button_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textview_first" />
</androidx.constraintlayout.widget.ConstraintLayout>
Código java del primer fragmento (HomeFragment):
public class HomeFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.home, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(HomeFragment.this)
.navigate(R.id.action_FirstFragment_to_SecondFragment);
}
});
}
}
Muchas gracias de antemano por la ayuda :)