分類
Android Android support library StackOverflow

V4 Support library Fragment.getLoaderManager() vs getActivity().getSupportLoaderManager()

小鴨有來電時撞到pat咁既野,之前未遇過之後又reproduce唔到,最近又無改過相關既Code –

java.lang.NullPointerException: Attempt to invoke virtual method 'com.headuck.headuckblocker.dblistview.DbRecord$DbRecordView com.headuck.headuckblocker.dblistview.DbListFragmentBase$ListRecyclerAdapter.swapData(com.headuck.headuckblocker.dblistview.DbRecord$DbRecordView)' on a null object reference
    at com.headuck.headuckblocker.dblistview.DbListFragmentBase.onLoadFinished(DbListFragmentBase.java:75)
    at com.headuck.headuckblocker.dblistview.DbListFragmentBase.onLoadFinished(DbListFragmentBase.java:27)
    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:476)
    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:444)
    at android.support.v4.content.Loader.deliverResult(Loader.java:126)
    at com.headuck.headuckblocker.dblistview.JunkDbLoader.onComplete(JunkDbLoader.java:182)
    at com.headuck.headuckblocker.dblistview.JunkDbLoader$ForceLoadHandler.onComplete(JunkDbLoader.java:204)
    at com.headuck.headuckblocker.dblistview.DbRecord$RecordQueryThread$1.run(DbRecord.java:342)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5534)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

意思係Loader load完野call back時,Fragment的Listview未initialize,但係個Loader明明係遲過Fragment的Listview initialize的…

Trace一輪自己同suport library既source,仲計係因為在Fragment中用了唔知邊度抄返來的getActivity().getSupportLoaderManager()而唔係Fragment本身的getLoaderManager()去攞個LoaderManager,雖然兩者都係回覆同一個Loader Manager instance但係後者才會令Fragment在回收時處理個Loader。因此用了前者後,Fragment被回收時個Loader唔會被stop,而咁啱呢次Loader又做緊野…

API文件無特別提及呢點,因此之後去左StackOverflow寫低呢段野警世。(個accepted answer 自已list左段code已證明兩者不同,但又答話 I’m pretty sure there is no difference whatshoever between the two,真係…)

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *