gpt4 book ai didi

android - 如何使用导航从 bottomsheetdialog 导航到 fragment ?

转载 作者:行者123 更新时间:2023-12-03 22:00:30 26 4
gpt4 key购买 nike

我目前正在制作一个具有底页功能的应用程序,当用户点击对话框内的图标( ImageView )时, fragment 将显示出来。我的问题是我无法导航到 fragment b,它返回了这个错误 enter image description here

这是我的家庭 fragment , Activity 启动时首先开始的 fragment

public class HomeFrag extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
ImageView buttonOpenSheet = view.findViewById(R.id.buttonopensheet);

buttonOpenSheet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BottomSheetDialog bottomsheet = new BottomSheetDialog();
bottomsheet.show(getFragmentManager(), "bottomsheet");
}
});

return view;
}}

bottomsheetdialog.java
public class BottomSheetDialog extends BottomSheetDialogFragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.bottom_sheet, container, false);

//Views
ImageView usual = view.findViewById(R.id.usual);
final NavController navigator =
Navigation.findNavController(getActivity(), R.id.fragment_host);

usual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

navigator.navigate(R.id.action_bottomSheetDialog_to_usualFrag);
Log.d(TAG, "user clicked" );
}
});
return view;
}}

这是我的导航
<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/app_nav"
app:startDestination="@id/homeFrag">

<fragment
android:id="@+id/homeFrag"
android:name="com.bw.f22920.HomeFrag"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />

<dialog
android:id="@+id/bottomSheetDialog"
android:name="com.bw.f22920.BottomSheetDialog"
android:label="bottom_sheet"
tools:layout="@layout/bottom_sheet">
<action
android:id="@+id/action_bottomSheetDialog_to_usualFrag"
app:destination="@id/usualFrag" />
</dialog>
<fragment
android:id="@+id/usualFrag"
android:name="com.bw.f22920.UsualFrag"
android:label="fragment_usual"
tools:layout="@layout/fragment_usual" />


</navigation>

最佳答案

你正在使用

@Override
public void onClick(View v) {
BottomSheetDialog bottomsheet = new BottomSheetDialog();
bottomsheet.show(getFragmentManager(), "bottomsheet");
}

这意味着 NavController 不知道您当前的目的地已更改为底部工作表对话框。

您需要调用 navigate() :
@Override
public void onClick(View v) {
Navigation.findNavController(v).navigate(R.id.bottomSheetDialog);
}

关于android - 如何使用导航从 bottomsheetdialog 导航到 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59696375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com