尝试导航到 Android 项目中的另一个片段时出错

时间:2021-02-26 22:33:55

标签: android android-studio android-layout android-fragments

我正在使用 android studio 创建一个概念证明,并且我试图在片段之间创建一个过渡。我已经完成了下一个代码:

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);
    }
}

但是在.navigate();行抛出下一个错误:

<块引用>

无法从当前目标 Destination(administration.MyProject/addPartnerFragment) label=partnersFragmentLabel class=administration.MyProject.AddPartnerFragment 中找到导航操作/目标管理。MyProject/action_AddPartnerFragment

我的 nav_graph.xml 是下一个:

<?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>

看起来像这样:

nav_graph.xml

第一个转换(FirstFragment 到 SecondFragment 正常,但他接下来没有:(,如果两者都以相同的方式声明,我不知道为什么)

AddPartnerFragment 是下一个:

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));
    }
}

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>

还有 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>

预先感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

您没有分享 nav_graph 代码,它来自 AddPartnerFragment。请使用 nav_graph 代码编辑问题