#java #android #android-navigation
Вопрос:
У меня есть этот фрагмент навигационного потока: вверху -> меню ->> меню-подробно
Когда я перехожу к деталям меню, я получаю эту ошибку, и приложение выходит из строя:
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() {
}
}
Любая помощь в исправлении этого была бы очень признательна. Спасибо!
Комментарии:
1. Ваша проблема связана с модификатором доступа на уровне класса, чтобы сделать ваш класс дочерним
Fragment
, вам нужно будет сделать его общедоступным . Т. Е.public class MenuDetailFragment ...
2. спасибо тебе, ю! вот оно что!