Skip to content
Advertisement

If we change fragment before countdowntimer is finished, the application crashes in android

As soon as the application opens, I use a 2-second countdown timer. After 2 second I visible linearlayout and invisible progressbar. The reason I use this is because I can pull all data through firebase and assign it to the corresponding text boxes. However, if I pass to another activity during the countdown timer, the program crashes.

object : CountDownTimer(1000, 1000) {
            override fun onTick(p0: Long) {
            }

            override fun onFinish() {
                  linearLayout_profile.visibility = View.VISIBLE
                  progressBar_profile.visibility = View.INVISIBLE
            }
        }.start()
2020-12-10 00:50:24.118 1742-1742/com.burakergun.emre E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.burakergun.emre, PID: 1742
    java.lang.NullPointerException: linearLayout_profile must not be null
        at com.burakergun.emre.Fragment.ProfileFragment$onViewCreated$3.onFinish(ProfileFragment.kt:66)
        at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        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)
2020-12-10 00:50:24.185 1742-1742/com.burakergun.emre I/Process: Sending signal. PID: 1742 SIG: 9

I check the logcat and show the fatal error. The error says linearlayout_profile must be not null. How can linearlayout_profile be null. I dont understand that part.

Advertisement

Answer

linearlayout_profile is null because you navigated to another activity and there is no reference to linearlayout_profile and hence null.

You need to add null check to prevent the crash.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement