拍照后,点击相机界面上的checkmark
按钮,我得到了导致应用崩溃的异常信息:
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
仅当我尝试拍照时才会发生此异常,而当我从图库中选择图像时则不会发生。
作为异常指出,data
参数以null
的形式传递,而参数应为non-null
并指向kotlin文件功能。在研究了这个问题之后,我知道我需要将?
传递给上述参数。 (来源:this和this),但现在我得到
Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Intent?
我没有与Kotlin合作的经验,因此不确定这是否是我的正确解决方案。如果有人遇到这样的异常,我想知道如何解决此问题。
以下代码:
void _checkCameraPermission() {
PermissionHandler()
.checkPermissionStatus(PermissionGroup.camera)
.then((status) {
if (status == PermissionStatus.granted) {
getImage(ImageSource.camera);
} else {
_askCameraPermission();
}
});
}
void _askCameraPermission() {
PermissionHandler()
.requestPermissions([PermissionGroup.camera]).then(_onStatusRequested);
}
void _onStatusRequested(Map<PermissionGroup, PermissionStatus> value) {
final status = value[PermissionGroup.camera];
if (status == PermissionStatus.granted) {
getImage(ImageSource.camera);
}
}
Future uploadFile(imageFile) async {
// String fileName = DateTime.now().millisecondsSinceEpoch.toString();
String fileName = "images/" + new Uuid().v4() + ".jpg";
StorageReference reference = FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = reference.putFile(imageFile);
await uploadTask.onComplete.then((value) {
reference.getDownloadURL().then((result) {
// result is the file URL
if (result != null) {
setState(() {
isLoading = false;
});
onSendMessage(result, 2);
}
});
});
}
异常日志:
E/AndroidRuntime( 8402): FATAL EXCEPTION: main
E/AndroidRuntime( 8402): Process: com.quickcarl.qcflutterpro, PID: 8402
E/AndroidRuntime( 8402): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2343, result=-1, data=null} to activity {com.quickcarl.qcflutterpro/com.quickcarl.qcflutterpro.MainActivity}: **java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data**
E/AndroidRuntime( 8402): at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
E/AndroidRuntime( 8402): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
E/AndroidRuntime( 8402): at android.app.ActivityThread.-wrap16(ActivityThread.java)
E/AndroidRuntime( 8402): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
E/AndroidRuntime( 8402): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 8402): at android.os.Looper.loop(Looper.java:148)
E/AndroidRuntime( 8402): at android.app.ActivityThread.main(ActivityThread.java:5417)
E/AndroidRuntime( 8402): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 8402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
E/AndroidRuntime( 8402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/AndroidRuntime( 8402): Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
E/AndroidRuntime( 8402): at com.quickcarl.qcflutterpro.MainActivity.onActivityResult(MainActivity.kt)
E/AndroidRuntime( 8402): at android.app.Activity.dispatchActivityResult(Activity.java:6428)
E/AndroidRuntime( 8402): at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
E/AndroidRuntime( 8402): ... 9 more
MainActivity代码:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == VIDEO_SCREEN_REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
sendStringMessageToFlutter(data.getStringExtra("roomName"), "completeRoom")
}
}
}
答案 0 :(得分:1)
按如下所示更改onActivityResult
方法的签名:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}
这将解决问题。 data
可以为空,但您将其声明为non-null
编辑:
然后,在函数主体中,确保在检索字符串时检查可为空性:
if(resultCode == Activity.RESULT_OK) {
sendStringMessageToFlutter(data?.getStringExtra("roomName") ?: "default_value_when_null", "completeRoom")
}