gpt4 book ai didi

android - 导航组件,没有设置 NavController,在 androidx.navigation.Navigation.findNavController 没有设置 NavController

转载 作者:行者123 更新时间:2023-12-04 12:32:20 28 4
gpt4 key购买 nike

我基本上是在等待 5 秒后尝试导航到其他 fragment 。当我在 onViewCreated 中执行导航过程时,它可以工作,但是当我在 onFinish 中执行时,它给了我下面的错误。
我试图将 View 对象保留为全局变量,但它也不起作用。

E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.example.ui, PID: 17838java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{8dcd038 V.E...... .......D 0,0-1080,1908} does not have a NavController setat androidx.navigation.Navigation.findNavController(Navigation.java:84)at com.example.ui.sign_in.LoadingFragment$1.onFinish(LoadingFragment.java:39)at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:201)at android.app.ActivityThread.main(ActivityThread.java:6810)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)I/Process: Sending signal. PID: 17838 SIG: 9


** 私有(private) View View ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_loading, container, false);
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
this.view = view;

new CountDownTimer(5000, 1000) { //Wait 5 seconds
public void onTick(long millisUntilFinished) {
}

public void onFinish() { // Then navigate to other page.
navigate();
}
}.start();

super.onViewCreated(view, savedInstanceState);
}


private void navigate() {
Navigation.findNavController(this.view).navigate(LoadingFragmentDirections.actionLoadingFragmentToSignInPagerFragment());
}**

最佳答案

解决方案1)
您需要在 onFinish() 之前检查 fragment 是否可见。被叫
添加一个 bool 值,在 onResume() 中初始化它并在 onPause() 中设为假像这样

private boolean isFragmentVisible=false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_loading, container, false);
}



@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
this.view = view;

new CountDownTimer(5000, 1000) { //Wait 5 seconds
public void onTick(long millisUntilFinished) {
}

public void onFinish() { // Then navigate to other page.

//check if fragment is visible or not
if(isFragmentVisible){
navigate();
}

}
}.start();

super.onViewCreated(view, savedInstanceState);
}

@Override
public void onResume() {
super.onResume();
isFragmentVisible=true;
}

@Override
public void onPause() {
super.onPause();
isFragmentVisible=false;
}
private void navigate() {
Navigation.findNavController(this.view).navigate(LoadingFragmentDirections.actionLoadingFragmentToSignInPagerFragment());
}**
解决方案2)
您需要声明一个 CountDownTimer 实例并将其初始化一次,并在 fragment 不再可见时将其设为 null

关于android - 导航组件,没有设置 NavController,在 androidx.navigation.Navigation.findNavController 没有设置 NavController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64569577/

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