giandifra / arcore_flutter_plugin

Flutter plugin for ARCore SDK, Android platform to build new augmented reality experiences
MIT License
435 stars 280 forks source link

NullPointerException in ArCoreView on dispose and recreate #233

Open Akapohn opened 3 months ago

Akapohn commented 3 months ago

First, I would like to thank you for your excellent and helpful plugin. However, I found a problem where the app crashes when disposing and recreating the ArCoreView. Here are the error logs and the solution I found.

Error Logs

E/flutter (11682): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, null cannot be cast to non-null type android.view.View, null, java.lang.NullPointerException: null cannot be cast to non-null type android.view.View
E/flutter (11682):      at com.difrancescogianmarco.arcore_flutter_plugin.ArCoreView.getView(ArCoreView.kt:512)
E/flutter (11682):      at io.flutter.plugin.platform.VirtualDisplayController.getView(VirtualDisplayController.java:278)
E/flutter (11682):      at io.flutter.plugin.platform.PlatformViewsController$1.dispose(PlatformViewsController.java:260)
E/flutter (11682):      at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.dispose(PlatformViewsChannel.java:150)
E/flutter (11682):      at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:58)
E/flutter (11682):      at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/flutter (11682):      at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/flutter (11682):      at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/flutter (11682):      at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter (11682):      at android.os.Handler.handleCallback(Handler.java:958)
E/flutter (11682):      at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter (11682):      at android.os.Looper.loopOnce(Looper.java:230)
E/flutter (11682):      at android.os.Looper.loop(Looper.java:319)
E/flutter (11682):      at android.app.ActivityThread.main(ActivityThread.java:8919)
E/flutter (11682):      at java.lang.reflect.Method.invoke(Native Method)
E/flutter (11682):      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
E/flutter (11682):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
E/flutter (11682): )

Revise getView() Function in ArCoreView.kt and BaseArCoreView.kt

override fun getView(): View? {
        return arSceneView as? View
}
Joshokelola commented 3 months ago

Hi, I'm experiencing the same issue.How do i implement this change on my own end?

Akapohn commented 3 months ago

@Joshokelola Sorry, I forgot to provide the file name and path. Locate the plugin in the local pub cache and go to .\android\src\main\kotlin\... Then, revise the function in ArCoreView.kt and BaseArCoreView.kt.

prachishukla007 commented 1 month ago

Do we have any solution of it?

darshan-vanol commented 1 week ago

Thanks! it worked! 👍