GCX-HCI / ThirtyInch

a MVP library for Android favoring a stateful Presenter
Apache License 2.0
1.03k stars 101 forks source link

IllegalStateException: The view is currently not attached. Caused by click listeners #104

Closed ar-g closed 7 years ago

ar-g commented 7 years ago

We have in our crashlytics some NPE crashes caused by onClickListeners in ViewHolders and Fragments. They ain't many but still occurs. Do you have any idea why?

Fatal Exception: java.lang.IllegalStateException: The view is currently not attached. Use 'sendToView(ViewAction)' instead.
       at net.grandcentrix.thirtyinch.f.Q(TiPresenter.java:334)
       at classifieds.yalla.features.chats.chat.regular.i.p(ChatPresenter.java:207)
       at classifieds.yalla.features.chats.chat.regular.ChatFragment.b(ChatFragment.java:109)
       at classifieds.yalla.features.chats.chat.regular.a.onClick(Unknown Source)
       at android.view.View.performClick(View.java:4475)
       at android.view.View$PerformClick.run(View.java:18786)
       at android.os.Handler.handleCallback(Handler.java:730)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:176)
       at android.app.ActivityThread.main(ActivityThread.java:5419)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:525)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
       at dalvik.system.NativeStart.main(NativeStart.java)
passsy commented 7 years ago

You're running asynchronous code in your presenter. The user removed your fragment somehow (or closed the app) and the view was detached (onDetachView()). Then your async code returns and calls getViewOrThrow(). The view is detached and null.

As mentioned in the error message you should use sendToView(v -> v.callYourMethod()) instead of getViewOrThrow().callYourMethod()