使用片段导航两个级别会导致应用程序崩溃

rsl1atfo  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(250)

我有这个片段导航流程:顶部->菜单->菜单细节
当我进入菜单详细信息时,出现以下错误,应用程序崩溃:

2021-07-08 23:10:26.677 13975-13975/com.example.cafe E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cafe, PID: 13975
    androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.cafe.menudetail.MenuDetailFragment: make sure class name exists, is public, and has an empty constructor that is public
        at androidx.fragment.app.Fragment.instantiate(Fragment.java:624)
        at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
        at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483)
        at androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:132)
        at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:162)
        at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58)
        at androidx.navigation.NavController.navigate(NavController.java:1066)
        at androidx.navigation.NavController.navigate(NavController.java:944)
        at androidx.navigation.NavController.navigate(NavController.java:877)
        at androidx.navigation.NavController.navigate(NavController.java:863)
        at androidx.navigation.NavController.navigate(NavController.java:851)
        at com.example.cafe.drinks.DrinkRecyclerViewAdapter.lambda$onBindViewHolder$0(DrinkRecyclerViewAdapter.java:41)
        at com.example.cafe.drinks.-$$Lambda$DrinkRecyclerViewAdapter$OKcOlCT8PNli-NMx_JQFcxC9Bpc.onClick(Unknown Source:0)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7664)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.IllegalAccessException: java.lang.Class<com.example.cafe.menudetail.MenuDetailFragment> is not accessible from java.lang.Class<androidx.fragment.app.Fragment>
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at androidx.fragment.app.Fragment.instantiate(Fragment.java:613)
        at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) 
        at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483) 
        at androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:132) 
        at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:162) 
        at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58) 
        at androidx.navigation.NavController.navigate(NavController.java:1066) 
        at androidx.navigation.NavController.navigate(NavController.java:944) 
        at androidx.navigation.NavController.navigate(NavController.java:877) 
        at androidx.navigation.NavController.navigate(NavController.java:863) 
        at androidx.navigation.NavController.navigate(NavController.java:851) 
        at com.example.cafe.drinks.DrinkRecyclerViewAdapter.lambda$onBindViewHolder$0(DrinkRecyclerViewAdapter.java:41) 
        at com.example.cafe.drinks.-$$Lambda$DrinkRecyclerViewAdapter$OKcOlCT8PNli-NMx_JQFcxC9Bpc.onClick(Unknown Source:0) 
        at android.view.View.performClick(View.java:7448) 
        at android.view.View.performClickInternal(View.java:7425) 
        at android.view.View.access$3600(View.java:810) 
        at android.view.View$PerformClick.run(View.java:28305) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7664) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2021-07-08 23:10:32.281 13975-13992/com.example.cafe W/System: A resource failed to call release. 
2021-07-08 23:10:32.282 13975-13992/com.example.cafe I/chatty: uid=10283(com.example.cafe) FinalizerDaemon identical 1 line
2021-07-08 23:10:32.282 13975-13992/com.example.cafe W/System: A resource failed to call release. 
2021-07-08 23:11:06.036 13975-13975/com.example.cafe I/Process: Sending signal. PID: 13975 SIG: 9

当我将流程更改为:顶部->菜单->顶部
它很好用。所以我的直觉是这与菜单细节片段本身有关。部分守则如下:
导航图

<?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/navigation"
            app:startDestination="@id/topLevelFragment">

    <fragment android:id="@+id/topLevelFragment" android:name="com.example.cafe.toplevel.TopLevelFragment"
              android:label="fragment_top_level_list" tools:layout="@layout/fragment_top_level_list">
        <action android:id="@+id/action_topLevelFragment_to_drinkFragment" app:destination="@id/drinkFragment"/>
    </fragment>
    <fragment android:id="@+id/drinkFragment" android:name="com.example.cafe.drinks.DrinkFragment"
              android:label="fragment_drink_list" tools:layout="@layout/fragment_drink_list">
        <action android:id="@+id/action_drinkFragment_to_menuDetailFragment3" app:destination="@id/menuDetailFragment"/>
    </fragment>
    <fragment android:id="@+id/menuDetailFragment" android:name="com.example.cafe.menudetail.MenuDetailFragment"
              android:label="fragment_menu_detail" tools:layout="@layout/fragment_menu_detail"/>
</navigation>

菜单详细信息片段布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".menudetail.MenuDetailFragment">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:text="@string/large_text"/>
</androidx.core.widget.NestedScrollView>

菜单细节片段

package com.example.cafe.menudetail;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.cafe.R;

class MenuDetailFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_menu_detail, container, false);
    }
    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public MenuDetailFragment() {
    }
}

如果您能帮助解决此问题,我们将不胜感激。谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题