記錄開發(fā)中碰到的錯(cuò)誤
把一些碰到的錯(cuò)誤都記錄下來踩衩。隨時(shí)更新吧衙传!
好記性,不如爛筆頭菱父。
No resource found that matches the given name
錯(cuò)誤如下
Error:(11, 31) No resource found that matches the given name (at 'layout_above' with value '@id/tv_base').
emmm碰到的一個(gè)奇葩。
代碼上面明明寫了但是就是找不到資源
出錯(cuò)代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_img_base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/tv_base"
android:padding="20dp"
app:srcCompat="@drawable/ic_launcher" />
<ImageView
android:id="@+id/iv_msg_base"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_margin="30dp"
app:srcCompat="@drawable/point_sel" />
<TextView
android:id="@+id/tv_base"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="20dp"
android:gravity="center"
android:text="" />
</RelativeLayout>
出錯(cuò)原因
是因?yàn)榫幾g的時(shí)候先編譯了上面的iv_img_base導(dǎo)致編譯的時(shí)候去讀取tv_base并未找到。
解決方案
只需要把tv_base的位置放到iv_img_base之前就解決了浙宜!
其實(shí)只是個(gè)很簡(jiǎn)單的問題官辽。但是確實(shí)會(huì)很容易碰到
多渠道打包出錯(cuò)(All flavors must now belong to a named flavor dimension.)
錯(cuò)誤如下
Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
出錯(cuò)代碼
productFlavors{
dev{
}
free{
applicationIdSuffix ".free"
buildConfigField("String", "Host", "\"Free版\"")
resValue("string", "app_name1", "myModefree")
}
}
出錯(cuò)原因
這是因?yàn)锳ndroid Studio3.0之后對(duì)多版本的versionCode控制,需要讓版本的code都一致粟瞬。
解決方案
在每個(gè)版本后面加入
flavorDimensions("versionCode")
如上面的錯(cuò)誤 修改為
productFlavors{
dev{
flavorDimensions("versionCode")
}
free{
applicationIdSuffix ".free"
buildConfigField("String", "Host", "\"Free版\"")
resValue("string", "app_name1", "myModefree")
flavorDimensions("versionCode")
}
}
或者在productFlavors之后加入
productFlavors{
.....
}
productFlavors.all{
flavorDimensions("versionCode")
}
Gradle修改打包名字錯(cuò)誤(AS3.0版本)
出錯(cuò)如下
Error:(35, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=devDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
出錯(cuò)代碼
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')){
def fileName = "Test_${defaultConfig.versionName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
出錯(cuò)原因
上面這句話在AS3.0版本之前是完全正常的同仆,就是設(shè)置打包的app為Test_版本號(hào).apk的包。
但是在AS3.0上面就會(huì)報(bào)錯(cuò)亩钟,是因?yàn)锳S3.0之后這塊簡(jiǎn)化了這句話乓梨,可以用更簡(jiǎn)單的代碼實(shí)現(xiàn)
解決方案
修改代碼
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "Test_${variant.versionName}.apk"
}
}
轉(zhuǎn)自原答案
Error:Execution failed for task ':mergeDebugResources'. >
錯(cuò)誤如下
Error:Execution failed for task ‘:mergeDebugResources’. > Some file crunching failed, see logs for details/
出錯(cuò)原因
mergeDebugResources在檢索資源圖片的時(shí)候,部分資源圖片出錯(cuò)導(dǎo)致編譯不過清酥。找到圖片修改或者讓編譯不去驗(yàn)證圖片的正確性扶镀!
解決方案
在app的build.gradle的android下加入
aaptOptions{
cruncherEnabled false
useNewCruncher false
}
如果還是會(huì)出現(xiàn)錯(cuò)誤就大部分是個(gè)別.9文件錯(cuò)誤。打開Android Studio右下角的Gradle Console 查看錯(cuò)誤原因焰轻,修改.9文件
Error:Execution failed for task':transformClassesWithDexForDebug'.>
出錯(cuò)如下
Error:Execution failed for task ':transformClassesWithDexForDebug'.>
com.android.build.api.transform.TransformException:
com.android.ide.common.process.ProcessException:
java.util.concurrent.ExecutionException: com.android.dex.DexException:
Multiple dex files define Landroid/support/v4/media/TransportController;
錯(cuò)誤原因
這個(gè)是eclipse轉(zhuǎn)Android Studio項(xiàng)目的時(shí)候碰到的一個(gè)bug臭觉,由于eclipse項(xiàng)目都有自己的v4包或者引用了別的v7包,然后在Android Studio中又compile了v4和v7導(dǎo)致的錯(cuò)誤辱志。
解決方案
刪除libs下的v4和v7包就可以了蝠筑。使用compile來管理v4和v7包!
關(guān)于EditText設(shè)置屬性setEnabled(false)之后再setEnabled(true)回來還是無法編輯
錯(cuò)誤原因
由于設(shè)置setEnabled(false)之后其實(shí)是把EditText的大部分屬性都設(shè)置成了false
在setEnabled(true)設(shè)置回來其實(shí)是不會(huì)把關(guān)于編輯和光標(biāo)的屬性設(shè)置回來的揩懒。需要手動(dòng)在設(shè)置一下什乙。
解決方案
//設(shè)置禁止編輯以及點(diǎn)擊等事件
EditText.setEnabled(false);
//設(shè)置允許和獲取光標(biāo)已經(jīng)編輯
EditText.setEnabled(true);
EditText.setFocusableInTouchMode(true);
關(guān)于打包報(bào)錯(cuò)not translated
出錯(cuò)如下
Error: "xxxxxxxx" is not translated in "ar" (Arabic), "cs" (Czech), "de" (German), "es" (Spanish), "fi" (Finnish), "fr" (French), "he" (Hebrew), "it" (Italian), "iw" (Hebrew), "ja" (Japanese), "ko" (Korean), "nl" (Dutch), "pl" (Polish), "pt" (Portuguese), "pt-BR" (Portuguese: Brazil), "ro" (Romanian), "ru" (Russian), "zh" (Chinese) [MissingTranslation]
...
其中xxx是文件名
出錯(cuò)原因
看錯(cuò)誤是編譯器不能識(shí)別,到底改xml屬于哪個(gè)語言的xml文件已球,所以打包的時(shí)候報(bào)錯(cuò)臣镣,而編譯運(yùn)行的時(shí)候不會(huì)出現(xiàn)。
解決方案
在項(xiàng)目的主項(xiàng)目build.gradle中加入
android{
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
慢慢更新吧智亮!續(xù)