[Android] Fix Bug: java.lang.IllegalStateException:Fragment not attached to Activity


再切換Fragment時,調用getResource()導致異常問題 java.lang.IllegalStateException:Fragment not attached to Activity

解決方法:
getResource()時,加入isAdded()去判斷Fragment is attached to Activity

if (isAdded()) {
         mSurfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);
}


相關解決方法參考:
1. https://stackoverflow.com/questions/36745309/accessing-resources-in-fragment-background-task-returns-illegalstateexception-of

留言

這個網誌中的熱門文章

[Android] TextView 換行

[Android]android Global variable 寫法

[Android] build the JAR file in Android Studio