search

Home  >  Q&A  >  body text

android - When the phone is in landscape or portrait mode, the instance in the fragment such as the recyclerview object is null, and the callback when returning data in the background causes a null pointer exception.

When the mobile phone switches between horizontal and vertical screens, the fragment status is lost, causing a null pointer exception to be reported when the data callback requested by the child thread is displayed. How to solve it

阿神阿神2799 days ago1186

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-05-31 10:40:57

    It feels like screen rotation triggers redrawing. Try adding the following attributes to the current Activity in Manifast:
    android:configChanges="orientation|screenSize"

    reply
    0
  • 为情所困

    为情所困2017-05-31 10:40:57

    Whether you are rotating the screen or closing Activity, you should cancel the response monitoring of the network interface when Fragment#onDestroy() or Fragment#onDestroyView(). Otherwise, even if it is solved by setting android:configChanges When the screen is rotated, Activity is rebuilt and causes the App to crash. Other scenarios may still cause the App to crash.

    It is recommended to read the Android source code to understand the following knowledge points:

      How
    1. Activity manages Fragment

    2. How
    3. Fragment manages View

    4. How
    5. Activity & Fragment manage Loader

    It is also recommended to use Retrofit + RxAndroid + RxLifecycle to implement network interfaces and calls. For page data management, you can consider Loader.

    reply
    0
  • Cancelreply