美文网首页
android Color.parseColor("颜色字符串"

android Color.parseColor("颜色字符串"

作者: hao_developer | 来源:发表于2020-11-19 18:03 被阅读0次

    错误信息:

     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dsy.test/com.dsy.test.SlideViewActivity}: java.lang.NumberFormatException: For input string: "AARRGGBB"
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3894)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4077)
            at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
            at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
            at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2458)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:219)
            at android.app.ActivityThread.main(ActivityThread.java:8387)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
         Caused by: java.lang.NumberFormatException: For input string: "AARRGGBB"
            at java.lang.Long.parseLong(Long.java:594)
            at android.graphics.Color.parseColor(Color.java:1386)
            at com.dsy.test.SlideViewActivity.onCreate(SlideViewActivity.kt:21)
            at android.app.Activity.performCreate(Activity.java:8121)
            at android.app.Activity.performCreate(Activity.java:8109)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3867)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4077) 
            at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
            at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
            at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2458) 
            at android.os.Handler.dispatchMessage(Handler.java:110) 
            at android.os.Looper.loop(Looper.java:219) 
            at android.app.ActivityThread.main(ActivityThread.java:8387) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055) 
    

    Android的Color.parseColor("颜色字符串")这个方法以下是错误的案例

    val color = Color.parseColor("#AARRGGBB")
    val color = Color.parseColor("#fff")
    val color = Color.parseColor("#f0f0")
    val color = Color.parseColor("#123")
    

    当时线上砸了,ios没问题,android一打开就闪退,吓得我一身的冷汗,最后确定是运营哪里配置色值有问题,Color.parseColor("#AARRGGBB")一转化就崩溃

    最后做了一下优化:

     val color = strToHextColor("#AARRGGBB","#FCFCFC")
    rlLay.setBackgroundColor(color)
    
    /**
    *@color:后台返回的色值
    *@defaultColor:ui默认的色值
    */
    fun strToHextColor(color: String?,defaultColor:String): Int {
       if (TextUtils.isEmpty(color)){
           return Color.parseColor(defaultColor)
       }
       if (!TextUtils.isEmpty(color) && (color?.length ?: 0 == 7) && (color?.startsWith("#") == true)) {
           return Color.parseColor(color)
       }else{
          return Color.parseColor(defaultColor)
       }
    }
    

    相关文章

      网友评论

          本文标题:android Color.parseColor("颜色字符串"

          本文链接:https://www.haomeiwen.com/subject/yzyliktx.html