简体   繁体   English

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

[英]Parameter specified as non-null is null caused by java.lang.IllegalArgumentException

After I take a picture and click checkmark button on camera interface, I get below exception that leads to app crash: 拍照后,单击相机界面上的checkmark按钮,出现以下异常,导致应用崩溃:

Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data

This exception occurs only if I try to take a picture, not when I select an image from the gallery. 仅当我尝试拍照时才会发生此异常,而当我从图库中选择图像时则不会发生。

As the exception states that the data parameter is being passed as null whereas it should be non-null and points to the kotlin file function. 例外情况是, data参数将作为null传递,而应该non-null并指向kotlin文件函数。 After researching on this issue, I came to know that I need to pass ? 在研究了这个问题之后,我才知道我需要通过? to the said parameter. 到所述参数。 (Source : this and this ), but now I am getting (来源: thisthis ),但是现在我得到了

Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Intent?

I have no experience of working with Kotlin so am not sure if that was the correct fix in my case. 我没有与Kotlin合作的经验,因此不确定这是否是我的正确解决方案。 I would like to know how to fix this issue if anybody has come across an exception like this. 如果有人遇到这样的异常,我想知道如何解决此问题。

Code below: 代码如下:

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);
        }
      });
    });
  }

Exception log: 异常日志:

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 code: 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")
            }
        }
    }

Change the signature of the onActivityResult method as follow: 如下更改onActivityResult方法的签名:

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

This will fix the problem. 这样可以解决问题。 data can be null but you're declaring it as non-null data可以为空,但您将其声明non-null


EDIT: 编辑:

Then, in the body of the function, be sure to check for nullability when retrieving the string: 然后,在函数主体中,确保在检索字符串时检查可为空性:

if(resultCode == Activity.RESULT_OK) {
   sendStringMessageToFlutter(data?.getStringExtra("roomName") ?: "default_value_when_null", "completeRoom")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException:指定为非空的参数是 Java 代码中的 null 错误 - java.lang.IllegalArgumentException: Parameter specified as non-null is null error in Java code 错误:java.lang.IllegalArgumentException:指定为非null的参数为空Firebase事务Kotlin - Error: java.lang.IllegalArgumentException: Parameter specified as non-null is null firebase transaction kotlin java.lang.IllegalArgumentException:指定为非空的参数是 null:- android,应用程序启动但立即崩溃 - java.lang.IllegalArgumentException: Parameter specified as non-null is null: - android, app starting but crashing straight away java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull - java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull java.lang.IllegalArgumentException:纹理必须为非空(CanvasJs 和 JavaFX) - java.lang.IllegalArgumentException: Texture must be non-null (CanvasJs and JavaFX) 由 java.lang.IllegalArgumentException 引起:MediaButtonReceiver 组件可能不为空 - Caused by java.lang.IllegalArgumentException: MediaButtonReceiver component may not be null 原因:java.lang.IllegalArgumentException:主机名不能为null - Caused by: java.lang.IllegalArgumentException: Host name may not be null FusedLocationProviderClient 因 java.lang.NullPointerException 崩溃:指定为非空的参数位置为 null - FusedLocationProviderClient crash with java.lang.NullPointerException: parameter location specified as non-null is null JAXB给我:java.lang.IllegalArgumentException:参数不能为null - JAXB gives me: java.lang.IllegalArgumentException: is parameter must not be null HttpClient异常:java.lang.IllegalArgumentException:host参数为null - HttpClient exception: java.lang.IllegalArgumentException: host parameter is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM