kotlin/java/android/studio各种

742 阅读1分钟
  1. 错误记录

    Method addObserver must be called on the main thread
    

    查看调用堆栈,发现错误由路由调用navController.navigate触发,搜索stackoverflow发现有相似的问题 kotlin - Android Navigation Instrumentation test throws Method addObserver must be called on the main thread - Stack Overflow] 解决方案是以runOnUiThread包裹

    currentActivity.runOnUiThread {
        navController.setGraph(R.navigation.my_graph)
    }
    
  2. 错误记录

    val a = arrayOf<Uri>(imageUri)
    // Type mismatch: inferred type is Uri? but TypeVariable(T) was expected
    

    解决方式为

    if(imageUri!=null){
        val a = arrayOf<Uri>(imageUri)
    }
    
  3. 错误记录

    项目由最新的as创建, 代码来自安卓官网拍照的demo developer.android.com/training/ca…

    private fun handlerClick() {
        Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { intent ->
            intent.resolveActivity(packageManager)?.also {
                startActivityForResult(intent, REQUEST_IMAGE_CAPTURE)
            }
        }
    }
    
    

    然后在编译阶段会抛出一个无法查找到原因的错误,导致无法继续进行

    java.lang.NullPointerException: Cannot invoke "com.android.tools.r8.internal.Fg.c2()" because the return value of "com.android.tools.r8.internal.Fg.K()" is null
    

    2021年9月6日17:41:00

     增加记录
    

    2021年09月07日09:05:57

     新增内容
     由于笔记本电源供电不足,关机,次日开机,问题没有重现
     
    

    结论

     不要在某些奇怪bug上消耗太多时间.
     
    
  4. 错误记录

    拍照Intent设置 captureIntent.putExtra(MediaStore.*EXTRA_OUTPUT*, imageUri)之后, onActivityResult第三个参数Intent为null

    解决方法为直接通过imageUri对应的path来读取文件

  5. 模拟器连接主机上得服务器请使用 10.0.2.2