1. 程式人生 > >Android 開發中常見bug 總結

Android 開發中常見bug 總結

密碼形式 EditText 的 Hint 字型顯示 異常

EditText password = (EditText) findViewById(R.id.register_password_text);
password.setTypeface(Typeface.DEFAULT);
password.setTransformationMethod(new PasswordTransformationMethod());

TextView設定ellipsize=”end”省略號後面還有字

 android:ellipsize="end"
 android:maxLines="2"

效果圖

原因:textview中包含了回車或者html字元
解決辦法:
- myText.replaceAll("\\<.*?>","")
或者
- Html.fromHtml(myText).toString())

warning: CRLF will be replaced by LF…..

在git提交的時候出現了這個錯誤,產生這個問題是因為在windows、Linux和Mac在處理檔案換行時的標示符不一致。windows使用CRLF作為結束符,而Linux和Mac使用LF作為結束符。

如果剛從windows轉到mac 開發,會遇到此錯誤,反之,則會出現warning: LF will be replaced by CRLF…..

解決辦法:我們可以通過terminal.app 或者cmd 檢視自己的git配置

$ git config core.autocrlf

如果顯示true,則git會在提交的時候自動幫我們處理換行。可通過如下程式碼更改設定

$ git config --global core.autocrlf  true

路徑錯誤

 Error:(1, 0) Your project path contains non-ASCII characters. This will most likely
cause the build to fail on Windows. Please move your project to a different
directory. See

http://b.android.com/95744 for details.

This warning can be disabled by using the command line flag
-Dcom.android.build.gradle.overridePathCheck=true, or adding the line
‘com.android.build.gradle.overridePathCheck=true’ to gradle.properties file
in the project directory.

匯入或者新建androidstudio專案的時候,路徑中包含了中文,去掉中文即可!!

java.lang.IllegalStateException: Cannot add header view to list – setAdapter has already been called.

解決方法:在setAdapter之前執行addHeaderView方法

public void addHeaderView(View v, Object data, boolean isSelectable) {  

    // 只要mAdapter不為空就丟擲此異常  
    if (mAdapter != null && ! (mAdapter instanceof HeaderViewListAdapter)) {  
        throw new IllegalStateException(  
                "Cannot add header view to list -- setAdapter has already been called.");  
    }  
  //......  
}  

TextView中英文混排提前換行的問題

這個問題是Android系統本身的bug,沒有很好的解決辦法,有說是全形半形的問題,有說用JustifyTextView可以解決問題,也有通過自定義控制元件解決問題的.比如如下這種情況

中英文混排提前換行

最終TextView中英文排版混亂 這篇博文在一定程度上解決了我的問題,感謝.遇到同樣問題的可以看看

主要思路是通過一個list將所有的字元存起來,再canvas.drawtext來解決問題.

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

丟擲這個異常的原因是試圖在activity的狀態已經儲存之後提交FragmentTransaction,導致activity狀態丟失.

解決建議:

  1. onCreate()中或者FragmentActivity#onResumeFragments()或者Activity#onPostResume()commit
  2. 避免在介面回掉中執行操作,如AsyncTask#onPostExecute()
  3. 使用commitAllowingStateLoss()來提交