开发中碰到的错误日记1

记录开发中碰到的错误

把一些碰到的错误都记录下来。随时更新吧!

好记性,不如烂笔头。

No resource found that matches the given name

错误如下

Error:(11, 31) No resource found that matches the given name (at 'layout_above' with value '@id/tv_base').

emmm碰到的一个奇葩。
代码上面明明写了但是就是找不到资源
出错代码

<?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>

出错原因
是因为编译的时候先编译了上面的iv_img_base导致编译的时候去读取tv_base并未找到。
解决方案
只需要把tv_base的位置放到iv_img_base之前就解决了!
其实只是个很简单的问题。但是确实会很容易碰到

多渠道打包出错(All flavors must now belong to a named flavor dimension.)

错误如下

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

出错代码

    productFlavors{
        dev{
            
        }
        free{
            applicationIdSuffix ".free"
            buildConfigField("String", "Host", "\"Free版\"")
            resValue("string", "app_name1", "myModefree")
        }
    }

出错原因
这是因为Android Studio3.0之后对多版本的versionCode控制,需要让版本的code都一致。
解决方案
在每个版本后面加入

flavorDimensions("versionCode")

如上面的错误 修改为

    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修改打包名字错误(AS3.0版本)

出错如下

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.

出错代码

            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)
                    }
                }
            }

出错原因
上面这句话在AS3.0版本之前是完全正常的,就是设置打包的app为Test_版本号.apk的包。
但是在AS3.0上面就会报错,是因为AS3.0之后这块简化了这句话,可以用更简单的代码实现
解决方案
修改代码

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "Test_${variant.versionName}.apk"
    }
}

转自原答案

Error:Execution failed for task ‘:mergeDebugResources’. >

错误如下

Error:Execution failed for task ‘:mergeDebugResources’. > Some file crunching failed, see logs for details/

出错原因
mergeDebugResources在检索资源图片的时候,部分资源图片出错导致编译不过。找到图片修改或者让编译不去验证图片的正确性!
解决方案
在app的build.gradle的android下加入

    aaptOptions{
        cruncherEnabled false
        useNewCruncher false
    }

如果还是会出现错误就大部分是个别.9文件错误。打开Android Studio右下角的Gradle Console 查看错误原因,修改.9文件

Error:Execution failed for task’:transformClassesWithDexForDebug’.>

出错如下

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;

错误原因
这个是eclipse转Android Studio项目的时候碰到的一个bug,由于eclipse项目都有自己的v4包或者引用了别的v7包,然后在Android Studio中又compile了v4和v7导致的错误。

解决方案
删除libs下的v4和v7包就可以了。使用compile来管理v4和v7包!

关于EditText设置属性setEnabled(false)之后再setEnabled(true)回来还是无法编辑

错误原因
由于设置setEnabled(false)之后其实是把EditText的大部分属性都设置成了false
在setEnabled(true)设置回来其实是不会把关于编辑和光标的属性设置回来的。需要手动在设置一下。

解决方案

        //设置禁止编辑以及点击等事件
        EditText.setEnabled(false);

        //设置允许和获取光标已经编辑
        EditText.setEnabled(true);
        EditText.setFocusableInTouchMode(true);

关于打包报错not translated

出错如下

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是文件名
出错原因
看错误是编译器不能识别,到底改xml属于哪个语言的xml文件,所以打包的时候报错,而编译运行的时候不会出现。
解决方案
在项目的主项目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
  }
}

慢慢更新吧!续

    原文作者:小南2017
    原文地址: https://www.jianshu.com/p/9d55425e9d12
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞