由java.lang.IllegalArgumentException引起的指定为非null的参数为null

问题描述 投票:0回答:1

在我拍照并点击相机界面上的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文件函数。在研究了这个问题之后,我开始知道我需要将?传递给上述参数。 (来源:thisthis),但现在我得到了

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")
            }
        }
    }
java android kotlin dart
1个回答
1
投票

更改onActivityResult方法的签名如下:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}

这将解决问题。 data可以为null,但你将其声明为non-null


编辑:

然后,在函数体中,确保在检索字符串时检查是否为空性:

if(resultCode == Activity.RESULT_OK) {
   sendStringMessageToFlutter(data?.getStringExtra("roomName") ?: "default_value_when_null", "completeRoom")
}
© www.soinside.com 2019 - 2024. All rights reserved.