lang.IllegalStateException: Fragment MainFragment{22904b9d} not attached to Activity
报错位置一般是在getString(R.string.xx)
原因是fragment 还未attach时调用content
解决办法
调用时判断
if(isAdded()){
getString(R.string.xx)
}
源码
如果该fragment当前添加到它的activity中,则返回真。
/** * Return true if the fragment is currently added to its activity. */ final public boolean isAdded() { return mHost != null && mAdded; }