在我的應用程序中,我有一個用于顯示數據的片段,以及一個用于更改應用程序中某些設置的設置活動。當我通過導航抽屜轉到設置活動,然后返回主屏幕(不更改設置)時,應用程序崩潰并出現以下異常:java.lang.IllegalStateException: Fragment NowWeatherFragment{b7914f8 (0e67ee0d-8776-45b5-9fd7-ee69841c31d1)} not attached to a context它似乎是在 API 調用響應之后發生的,并且有一個關于 SO 的另一個答案的合理解釋,但我嘗試過的任何事情都無法解決這個問題。我已經看到了其他幾個與此相關的問題,但我無法將這些問題的答案應用到我的情況中。我嘗試檢查 isAttached() 但這并沒有幫助,也對片段進行了空檢查,但它仍然遇到相同的異常與其發布所有課程,有些課程很長,這里是 github 存儲庫。它應該是相當輕量級的,并且很容易通過 Android Studio 在設備上運行 https://github.com/jollygreenegiant/SimpleWeather我希望該應用程序會返回主屏幕并像啟動時那樣顯示天氣數據。java.lang.IllegalStateException: Fragment NowWeatherFragment{b7914f8 (0e67ee0d-8776-45b5-9fd7-ee69841c31d1)} not attached to a context. at androidx.fragment.app.Fragment.requireContext(Fragment.java:765) at androidx.fragment.app.Fragment.getResources(Fragment.java:829) at com.jggdevelopment.simpleweather.fragments.NowWeatherFragment.setupViews(NowWeatherFragment.java:82) at com.jggdevelopment.simpleweather.fragments.NowWeatherFragment$3.onSharedPreferenceChanged(NowWeatherFragment.java:152) at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:612) at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:598) at com.jggdevelopment.simpleweather.fragments.MasterFragment.updateConditions(MasterFragment.java:263) at com.jggdevelopment.simpleweather.services.WeatherAPIUtils$2.onResponse(WeatherAPIUtils.java:92) at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
java.lang.IllegalStateException:API 響應后片段未附加到上下文
慕尼黑8549860
2023-03-17 10:19:11