unp*_*com 3 android kotlin kotlin-android-extensions
我有一个打开相机的活动,但是如果a在相机中,然后单击“后退”按钮,则每次都会出现此错误。
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)
Run Code Online (Sandbox Code Playgroud)
未能交付结果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()
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
之后,该应用程序停止工作。
您的代码:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
Run Code Online (Sandbox Code Playgroud)
有参数data永远不能为空,因为它具有类型Intent(不可为空)。
您的错误谈到了这一点:
指定为非null的参数为null:...参数数据
只需更改data: Intent为data: Intent?,data并将其指定为可为空的参数。
在Kotlin中阅读有关null安全的更多信息:文档。
| 归档时间: |
|
| 查看次数: |
428 次 |
| 最近记录: |