在利用Fragment做开发的时候,偶尔或者是少数情况下报错:Fragment XXFragment not attached to Activity,这有可能是因为Fragment在还没有Attach到Activity时,调用了如getResource()等,需要上下文Content的代码。因此在使用到上下文资源的地方加上:
if(isAdded()){
getResources().getString(R.string.app_name);
}
参考stackoverflow: http://stackoverflow.com/questions/10919240/fragment-myfragment-not-attached-to-activity
注:本人遇到这个问题也是在AsyncTask中使用了getResource,加上上述代码完美解决问题。