我基本上是在等待5秒后尝试导航到另一个片段。当我在onViewCreated中执行导航过程时,它可以正常工作,但是在onFinish中执行时会出现以下错误。
我尝试将View对象保留为全局变量,但也无效。
E/AndroidRuntime: 致命异常: 主要进程 进程: com.example.ui,PID:17838 java.lang.IllegalStateException: 视图androidx.constraintlayout.widget.ConstraintLayout {8dcd038 V.E.............D 0,0-1080,1908}没有设置NavController at 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:发送信号。 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());
}**