flutter_native_device_orientation
flutter_native_device_orientation copied to clipboard
"Attempt to invoke virtual method" occurs on NativeDeviceOrientationCommunicator.resume
When I call NativeDeviceOrientationCommunicator().resume()
, the following error occurs.
I tested on Platform: Android 12 Plugin version: 1.1.4
PlatformException (PlatformException(error, Attempt to invoke virtual method 'java.lang.String com.github.rmtmckenzie.native_device_orientation.NativeOrientation.name()' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.github.rmtmckenzie.native_device_orientation.NativeOrientation.name()' on a null object reference
at com.github.rmtmckenzie.native_device_orientation.NativeDeviceOrientationPlugin$StreamHandler$1.receive(NativeDeviceOrientationPlugin.java:115)
at com.github.rmtmckenzie.native_device_orientation.SensorOrientationListener.startOrientationListener(SensorOrientationListener.java:45)
at com.github.rmtmckenzie.native_device_orientation.NativeDeviceOrientationPlugin$MethodCallHandler.onMethodCall(NativeDeviceOrientationPlugin.java:86)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:319)
at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$TsixYUB5E6FpKhMtCSQVHKE89gQ.run(Unknown Source:12)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
))
Our product also encountered the same error.
- Platform: Android 8.0 - 13.0
- Plugin version: 1.1.4
- Flutter version: 3.3.3
We bumped the Flutter version to 3.3.3 and started having issues
PlatformException: PlatformException(error, Attempt to invoke virtual method 'java.lang.String java.lang.Enum.name()' on a null object reference, null, )
File "native_device_orientation.dart", line 55, in NativeDeviceOrientationCommunicator.resume
File "orientation_controller.dart", line 79, in _OrientationControllerImpl.resume
Same here. Crashes immediately, found this same error message via Sentry.
I think the timing of calling resume()
is the cause of the error.
The code that causes the problem is below from the error log.
https://github.com/rmtmckenzie/flutter_native_device_orientation/blob/be86db1b9e08f812bb76204d1c7525d5b5c5d6b8/android/src/main/java/com/github/rmtmckenzie/native_device_orientation/SensorOrientationListener.java#L45
resume()
is expected to be called after pause()
, but may be called otherwise depending on the application code implementation.
I want to add null check for lastOrientation
.
Same issue i am getting version native_device_orientation: ^1.2.1
Basic usage with NativeDeviceOrientationReader and it throws this error when camera plugin is used and user returns to my application.
in stream handler, there is receive method, that obviously receives null..
// initialize the callback. It is the same for both listeners. IOrientationListener.OrientationCallback callback = new IOrientationListener.OrientationCallback() { @Override public void receive(NativeOrientation orientation) { eventSink.success(orientation.name()); } };
E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.github.rmtmckenzie.native_device_orientation.NativeOrientation.name()' on a null object reference E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at com.github.rmtmckenzie.native_device_orientation.NativeDeviceOrientationPlugin$StreamHandler$1.receive(NativeDeviceOrientationPlugin.java:115) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at com.github.rmtmckenzie.native_device_orientation.SensorOrientationListener.startOrientationListener(SensorOrientationListener.java:45) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at com.github.rmtmckenzie.native_device_orientation.NativeDeviceOrientationPlugin$MethodCallHandler.onMethodCall(NativeDeviceOrientationPlugin.java:86) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at android.os.Handler.handleCallback(Handler.java:938) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at android.os.Handler.dispatchMessage(Handler.java:99) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at android.os.Looper.loopOnce(Looper.java:226) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at android.os.Looper.loop(Looper.java:313) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at android.app.ActivityThread.main(ActivityThread.java:8751) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) E/MethodChannel#com.github.rmtmckenzie/flutter_native_device_orientation/orientation(27511): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Hmm, that's weird. @rpelant can you give the exact steps you've followed to reproduce that? And you're saying you're using the camera plugin and the user is returning to the app - do you mean that the camera is started, the app is closed, then on returning to the app that's when this error is seen?
If at all possible it'd be great if you could create a project that is able to reproduce this, i.e. with the example app.
Sorry not to answer sooner, I have upgraded to version 2.0.3 and the error is now the same but with different call stack (why the callstack looks so weird? debug symbols missing?). The error is collected from crashlytics in production, on my development phone I am unable to reproduce. This error appears on different phone models and Android versions.
Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: PlatformException(error, Attempt to invoke virtual method 'java.lang.String java.lang.Enum.name()' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Enum.name()' on a null object reference at com.github.rmtmckenzie.native_device_orientation.d.l(Unknown Source:0) at com.github.rmtmckenzie.native_device_orientation.d.k(Unknown Source:0) at com.github.rmtmckenzie.native_device_orientation.b.a(Unknown Source:2) at com.github.rmtmckenzie.native_device_orientation.i.a(Unknown Source:8) at com.github.rmtmckenzie.native_device_orientation.d.a(Unknown Source:69) at x6.k$a.a(Unknown Source:17) at l6.c.l(Unknown Source:18) at l6.c.m(Unknown Source:40) at l6.c.i(Unknown Source:0) at l6.b.run(Unknown Source:12) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:230) at android.os.Looper.loop(Looper.java:319) at android.app.ActivityThread.main(ActivityThread.java:8893) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103) ) at StandardMethodCodec.decodeEnvelope(message_codecs.dart:651) at MethodChannel._invokeMethod(platform_channel.dart:322) at MethodChannelNativeDeviceOrientation.resume(native_device_orientation_method_channel.dart:60)
I use Sensor orientation and tried to find out from where nulls can come in SensorOrientationListener.java
only place where orientation is null is in initializer, can that be initialized with NativeOrientation.Unknown ?
private NativeOrientation lastOrientation = null;
and there it is used
@Override public void startOrientationListener() { if (orientationEventListener != null) { callback.receive(lastOrientation); return; } . . .