发送失败结果ResultInfo继续抛出

时间:2018-05-01 07:10:45

标签: android kotlin kotlin-android-extensions

我有一个打开相机的活动,但如果相机中有一个并且点击后退按钮,则每次都会出现此错误。

Process: com.deraah.mohamed.deraahpro, PID: 13346
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity {com.deraah.mohamed.deraahpro/com.deraah.mohamed.deraahpro.ParticipantsActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4268)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
    at android.app.ActivityThread.-wrap19(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
    at com.deraah.mohamed.deraahpro.ParticipantsActivity.onActivityResult(Unknown Source:37)

发送失败结果ResultInfo继续投掷时间。

这是我的活动ParticipantsActivity

class ParticipantsActivity : AppCompatActivity() {

    private var CAMERA_REQUEST = 1888
    private var imageView: ImageView? = null
    private var MY_CAMERA_PERMISSION_CODE = 100

    @RequiresApi(Build.VERSION_CODES.M)
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_participants)
        this.imageView = this.findViewById<View>(R.id.imageView1) as ImageView
        val photoButton = this.findViewById(R.id.button1) as Button

        photoButton.setOnClickListener { view ->
            if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(arrayOf(Manifest.permission.CAMERA),
                        MY_CAMERA_PERMISSION_CODE)
            } else {
                val cameraIntent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
                startActivityForResult(cameraIntent, CAMERA_REQUEST)
            }
        }

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        if (requestCode == MY_CAMERA_PERMISSION_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
                val cameraIntent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
                startActivityForResult(cameraIntent, CAMERA_REQUEST)
            } else {
                Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
            }

        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        if(data != null){
            if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
                try {

                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }
        }

    }

}

然后应用程序停止工作。

1 个答案:

答案 0 :(得分:5)

您的代码:

0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s

参数override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { 永远不应为null,因为它的类型为data(不可为空)。

您的错误说明了这一点:

  

指定为非null的参数为null:...参数数据

只需将Intent更改为data: Intentdata: Intent?将被指定为可为空的参数。

在Kotlin中阅读更多关于无效安全的内容:documentation