【monkey測試】Fragment not attached to Activity

monkey測試跑出了一個異常:html

// CRASH: packgeName (pid)

// Short Msg: java.lang.IllegalStateException

// Long Msg: java.lang.IllegalStateException: Fragment a{422d39d8} not attached to Activity

通過分析和定位,Fragment中一處加載資源報錯。java

緣由是這樣的Fragment在Activity中加載前就調用了,獲取資源的一些方法。android

如:api

getResources().getString(R.string.app_name);

如何解決問題呢?app

在Fragment中有 isAdded()這個方法,只須要在加載資源前進行判斷便可:測試

if(isAdded()){
        getResources().getString(R.string.app_name);
    }

 

isAdded()官方解釋:spa

final boolean isAdded()
Return true if the fragment is currently added to its activity.

 

解決問題參考:http://stackoverflow.com/questions/10919240/fragment-myfragment-not-attached-to-activitycode

相關文章
相關標籤/搜索