1. 程式人生 > >Android開發bug總結(持續更新)

Android開發bug總結(持續更新)

Android Studio bug總結

1.Error:Could not read cache value from 'C:\Users\xxxxx\.gradle\daemon\4.4\registry.bin'.
解決方法:開啟C盤當前使用者資料夾->.gradle->daemon->選擇報錯得gradle版本的資料夾->選中資料夾中所有檔案並刪除->重啟studio
參考連結:https://stackoverflow.com/questions/36664143/could-not-read-cache-value-from-registry-bin-android-studio

開發中bug總結

java.lang.IllegalStateException
Fragment XXXFragment not attached to a context.
字面意思理解,fragment無法和上下文關聯。其實就是fragment還沒有新增到activity中,我在程式碼中使用了getResources(),由於一些原因fragment還沒有新增到activity時就呼叫了該方法,導致報錯,看原始碼:


 /**
     * Return <code>requireActivity().getResources()</code>.
     */
@NonNull final public Resources getResources() { return requireContext().getResources(); } /** * Return the {@link Context} this fragment is currently associated with. * * @throws IllegalStateException if not currently associated with a context. * @see #getContext() */
@NonNull public final Context requireContext() { Context context = getContext(); if (context == null) { throw new IllegalStateException("Fragment " + this + " not attached to a context."); } return context; }

在context為空時會爆出該異常
解決辦法:使用fragment自帶方法isAdd(),新增判斷即可

 /**
     * Return true if the fragment is currently added to its activity.
     */
    final public boolean isAdded() {
        return mHost != null && mAdded;
    }