Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Application crashes when displaying a PDFViewer widget #100

Open
ncdm-stldr opened this issue Feb 1, 2022 · 6 comments
Open

Application crashes when displaying a PDFViewer widget #100

ncdm-stldr opened this issue Feb 1, 2022 · 6 comments

Comments

@ncdm-stldr
Copy link

Application crash with the following exception when displaying PDFViewer widget

[ +181 ms] I/System.out(25219): Already closed [ ] W/System.err(25219): java.lang.IllegalStateException: Already closed [ +6 ms] W/System.err(25219): at android.graphics.pdf.PdfRenderer.throwIfClosed(PdfRenderer.java:270) [ ] W/System.err(25219): at android.graphics.pdf.PdfRenderer.close(PdfRenderer.java:187) [ ] W/System.err(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin.getPage(FlutterPluginPdfViewerPlugin.java:206) [ ] W/System.err(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin.access$100(FlutterPluginPdfViewerPlugin.java:31) [ ] W/System.err(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin$1.run(FlutterPluginPdfViewerPlugin.java:84) [ ] W/System.err(25219): at android.os.Handler.handleCallback(Handler.java:883) [ ] W/System.err(25219): at android.os.Handler.dispatchMessage(Handler.java:100) [ ] W/System.err(25219): at android.os.Looper.loop(Looper.java:214) [ ] W/System.err(25219): at android.os.HandlerThread.run(HandlerThread.java:67) [ ] D/PdfViewerPlugin(25219): Retrieving page failed. [ ] D/AndroidRuntime(25219): Shutting down VM [ ] E/AndroidRuntime(25219): FATAL EXCEPTION: main [ ] E/AndroidRuntime(25219): Process: com.example.example, PID: 25219 [ ] E/AndroidRuntime(25219): java.lang.IllegalStateException: Reply already submitted [ ] E/AndroidRuntime(25219): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:286) [ ] E/AndroidRuntime(25219): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:267) [ ] E/AndroidRuntime(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin$1$2.run(FlutterPluginPdfViewerPlugin.java:92) [ ] E/AndroidRuntime(25219): at android.os.Handler.handleCallback(Handler.java:883) [ ] E/AndroidRuntime(25219): at android.os.Handler.dispatchMessage(Handler.java:100) [ ] E/AndroidRuntime(25219): at android.os.Looper.loop(Looper.java:214) [ ] E/AndroidRuntime(25219): at android.app.ActivityThread.main(ActivityThread.java:7399) [ ] E/AndroidRuntime(25219): at java.lang.reflect.Method.invoke(Native Method) [ ] E/AndroidRuntime(25219): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502) [ ] E/AndroidRuntime(25219): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980) [ +12 ms] E/flutter (25219): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method getPage on channel flutter_plugin_pdf_viewer) [ ] E/flutter (25219): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7) [ ] E/flutter (25219): <asynchronous suspension> [ ] E/flutter (25219): #1 PDFDocument.get (package:advance_pdf_viewer/src/document.dart:97:18) [ ] E/flutter (25219): <asynchronous suspension> [ ] E/flutter (25219): #2 _PDFViewerState._loadPage (package:advance_pdf_viewer/src/viewer.dart:151:18) [ ] E/flutter (25219): <asynchronous suspension> [ ] E/flutter (25219): [ +29 ms] I/Process (25219): Sending signal. PID: 25219 SIG: 9 [ +90 ms] Service protocol connection closed. [ ] Lost connection to device.

Steps to reproduce

I don't know if the issue is easy to reproduce, or if it happens is my specific situation.
Device plateform: Android
My flutter version: Flutter 2.8.1
Dart version: Dart 2.15.1
plugin version: advance_pdf_viewer: ^2.0.1
I simply use the PDFViewer in a regular fashion.

How to solve

After investigation the error seems two-folds but is easy to solve:
In file FlutterPluginPdfViewerPlugin.java

Integer pageNumber = call.<Integer>argument("pageNumber");
                                final String pageResult = getPage((String) call.argument("filePath"), pageNumber);
                                if (pageResult == null) {
                                    Log.d(TAG, "Retrieving page failed.");
                                    result.notImplemented();
                                }
                                mainThreadHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        result.success(pageResult);
                                    }
                                });

The if (pageResult == null) should be followed by a else block. This prevents using result two times which results in java.lang.IllegalStateException: Reply already submitted, because the method channel cannot receive two answers.
Adding the else block prevents the application from crashing in my case.

However, in my situation getPage still returns null, and 'result.notImplemented();' is called, which should not be the case.
So the second thing to fix is the getPage method which begin with:
try (PdfRenderer renderer = new PdfRenderer(getPdfFile(filePath))) {
This ensure the renderer is closed at the end of the try-catch. However, the renderer is also closed inside the try-catch, which should not be the case.
After removing the renderer.close(); call, I don't have the issue anymore.

I will create a pull request to provide the fix I just descibed above.

ncdm-stldr pushed a commit to ncdm-stldr/pdf_viewer that referenced this issue Feb 1, 2022
@udaykiranvl
Copy link

I have also faced this issue today after upgrading to

Flutter 2.8.1
Dart 2.15.1
advance_pdf_viewer: ^2.0.1

Thanks for the fix @ncdm-stldr

ncdm-stldr pushed a commit to ncdm-stldr/pdf_viewer that referenced this issue Feb 9, 2022
@weissloic
Copy link

Thanks for the fix !

@Hannan404
Copy link

Application crash with the following exception when displaying PDFViewer widget

[ +181 ms] I/System.out(25219): Already closed [ ] W/System.err(25219): java.lang.IllegalStateException: Already closed [ +6 ms] W/System.err(25219): at android.graphics.pdf.PdfRenderer.throwIfClosed(PdfRenderer.java:270) [ ] W/System.err(25219): at android.graphics.pdf.PdfRenderer.close(PdfRenderer.java:187) [ ] W/System.err(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin.getPage(FlutterPluginPdfViewerPlugin.java:206) [ ] W/System.err(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin.access$100(FlutterPluginPdfViewerPlugin.java:31) [ ] W/System.err(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin$1.run(FlutterPluginPdfViewerPlugin.java:84) [ ] W/System.err(25219): at android.os.Handler.handleCallback(Handler.java:883) [ ] W/System.err(25219): at android.os.Handler.dispatchMessage(Handler.java:100) [ ] W/System.err(25219): at android.os.Looper.loop(Looper.java:214) [ ] W/System.err(25219): at android.os.HandlerThread.run(HandlerThread.java:67) [ ] D/PdfViewerPlugin(25219): Retrieving page failed. [ ] D/AndroidRuntime(25219): Shutting down VM [ ] E/AndroidRuntime(25219): FATAL EXCEPTION: main [ ] E/AndroidRuntime(25219): Process: com.example.example, PID: 25219 [ ] E/AndroidRuntime(25219): java.lang.IllegalStateException: Reply already submitted [ ] E/AndroidRuntime(25219): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:286) [ ] E/AndroidRuntime(25219): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:267) [ ] E/AndroidRuntime(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin$1$2.run(FlutterPluginPdfViewerPlugin.java:92) [ ] E/AndroidRuntime(25219): at android.os.Handler.handleCallback(Handler.java:883) [ ] E/AndroidRuntime(25219): at android.os.Handler.dispatchMessage(Handler.java:100) [ ] E/AndroidRuntime(25219): at android.os.Looper.loop(Looper.java:214) [ ] E/AndroidRuntime(25219): at android.app.ActivityThread.main(ActivityThread.java:7399) [ ] E/AndroidRuntime(25219): at java.lang.reflect.Method.invoke(Native Method) [ ] E/AndroidRuntime(25219): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502) [ ] E/AndroidRuntime(25219): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980) [ +12 ms] E/flutter (25219): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method getPage on channel flutter_plugin_pdf_viewer) [ ] E/flutter (25219): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7) [ ] E/flutter (25219): <asynchronous suspension> [ ] E/flutter (25219): #1 PDFDocument.get (package:advance_pdf_viewer/src/document.dart:97:18) [ ] E/flutter (25219): <asynchronous suspension> [ ] E/flutter (25219): #2 _PDFViewerState._loadPage (package:advance_pdf_viewer/src/viewer.dart:151:18) [ ] E/flutter (25219): <asynchronous suspension> [ ] E/flutter (25219): [ +29 ms] I/Process (25219): Sending signal. PID: 25219 SIG: 9 [ +90 ms] Service protocol connection closed. [ ] Lost connection to device.

Steps to reproduce

I don't know if the issue is easy to reproduce, or if it happens is my specific situation. Device plateform: Android My flutter version: Flutter 2.8.1 Dart version: Dart 2.15.1 plugin version: advance_pdf_viewer: ^2.0.1 I simply use the PDFViewer in a regular fashion.

How to solve

After investigation the error seems two-folds but is easy to solve: In file FlutterPluginPdfViewerPlugin.java

Integer pageNumber = call.<Integer>argument("pageNumber");
                                final String pageResult = getPage((String) call.argument("filePath"), pageNumber);
                                if (pageResult == null) {
                                    Log.d(TAG, "Retrieving page failed.");
                                    result.notImplemented();
                                }
                                mainThreadHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        result.success(pageResult);
                                    }
                                });

The if (pageResult == null) should be followed by a else block. This prevents using result two times which results in java.lang.IllegalStateException: Reply already submitted, because the method channel cannot receive two answers. Adding the else block prevents the application from crashing in my case.

However, in my situation getPage still returns null, and 'result.notImplemented();' is called, which should not be the case. So the second thing to fix is the getPage method which begin with: try (PdfRenderer renderer = new PdfRenderer(getPdfFile(filePath))) { This ensure the renderer is closed at the end of the try-catch. However, the renderer is also closed inside the try-catch, which should not be the case. After removing the renderer.close(); call, I don't have the issue anymore.

I will create a pull request to provide the fix I just descibed above.

Application crash with the following exception when displaying PDFViewer widget

[ +181 ms] I/System.out(25219): Already closed [ ] W/System.err(25219): java.lang.IllegalStateException: Already closed [ +6 ms] W/System.err(25219): at android.graphics.pdf.PdfRenderer.throwIfClosed(PdfRenderer.java:270) [ ] W/System.err(25219): at android.graphics.pdf.PdfRenderer.close(PdfRenderer.java:187) [ ] W/System.err(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin.getPage(FlutterPluginPdfViewerPlugin.java:206) [ ] W/System.err(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin.access$100(FlutterPluginPdfViewerPlugin.java:31) [ ] W/System.err(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin$1.run(FlutterPluginPdfViewerPlugin.java:84) [ ] W/System.err(25219): at android.os.Handler.handleCallback(Handler.java:883) [ ] W/System.err(25219): at android.os.Handler.dispatchMessage(Handler.java:100) [ ] W/System.err(25219): at android.os.Looper.loop(Looper.java:214) [ ] W/System.err(25219): at android.os.HandlerThread.run(HandlerThread.java:67) [ ] D/PdfViewerPlugin(25219): Retrieving page failed. [ ] D/AndroidRuntime(25219): Shutting down VM [ ] E/AndroidRuntime(25219): FATAL EXCEPTION: main [ ] E/AndroidRuntime(25219): Process: com.example.example, PID: 25219 [ ] E/AndroidRuntime(25219): java.lang.IllegalStateException: Reply already submitted [ ] E/AndroidRuntime(25219): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:286) [ ] E/AndroidRuntime(25219): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:267) [ ] E/AndroidRuntime(25219): at pt.tribeiro.flutter_plugin_pdf_viewer.FlutterPluginPdfViewerPlugin$1$2.run(FlutterPluginPdfViewerPlugin.java:92) [ ] E/AndroidRuntime(25219): at android.os.Handler.handleCallback(Handler.java:883) [ ] E/AndroidRuntime(25219): at android.os.Handler.dispatchMessage(Handler.java:100) [ ] E/AndroidRuntime(25219): at android.os.Looper.loop(Looper.java:214) [ ] E/AndroidRuntime(25219): at android.app.ActivityThread.main(ActivityThread.java:7399) [ ] E/AndroidRuntime(25219): at java.lang.reflect.Method.invoke(Native Method) [ ] E/AndroidRuntime(25219): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502) [ ] E/AndroidRuntime(25219): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980) [ +12 ms] E/flutter (25219): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method getPage on channel flutter_plugin_pdf_viewer) [ ] E/flutter (25219): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7) [ ] E/flutter (25219): <asynchronous suspension> [ ] E/flutter (25219): #1 PDFDocument.get (package:advance_pdf_viewer/src/document.dart:97:18) [ ] E/flutter (25219): <asynchronous suspension> [ ] E/flutter (25219): #2 _PDFViewerState._loadPage (package:advance_pdf_viewer/src/viewer.dart:151:18) [ ] E/flutter (25219): <asynchronous suspension> [ ] E/flutter (25219): [ +29 ms] I/Process (25219): Sending signal. PID: 25219 SIG: 9 [ +90 ms] Service protocol connection closed. [ ] Lost connection to device.

Steps to reproduce

I don't know if the issue is easy to reproduce, or if it happens is my specific situation. Device plateform: Android My flutter version: Flutter 2.8.1 Dart version: Dart 2.15.1 plugin version: advance_pdf_viewer: ^2.0.1 I simply use the PDFViewer in a regular fashion.

How to solve

After investigation the error seems two-folds but is easy to solve: In file FlutterPluginPdfViewerPlugin.java

Integer pageNumber = call.<Integer>argument("pageNumber");
                                final String pageResult = getPage((String) call.argument("filePath"), pageNumber);
                                if (pageResult == null) {
                                    Log.d(TAG, "Retrieving page failed.");
                                    result.notImplemented();
                                }
                                mainThreadHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        result.success(pageResult);
                                    }
                                });

The if (pageResult == null) should be followed by a else block. This prevents using result two times which results in java.lang.IllegalStateException: Reply already submitted, because the method channel cannot receive two answers. Adding the else block prevents the application from crashing in my case.

However, in my situation getPage still returns null, and 'result.notImplemented();' is called, which should not be the case. So the second thing to fix is the getPage method which begin with: try (PdfRenderer renderer = new PdfRenderer(getPdfFile(filePath))) { This ensure the renderer is closed at the end of the try-catch. However, the renderer is also closed inside the try-catch, which should not be the case. After removing the renderer.close(); call, I don't have the issue anymore.

I will create a pull request to provide the fix I just descibed above.

Where this file "FlutterPluginPdfViewerPlugin.java" is exist in project?

@gtu-myowin
Copy link

gtu-myowin commented Feb 16, 2022

@Hannan404

It's not in your project. It's in the package.

Check this pull request out

Change your code according to that PR and hot restart your app. The error will be gone!

@joymyr
Copy link

joymyr commented Feb 16, 2022

Thanks. That fixed the crash for me. Hope it gets merged soon.

supnewmobile pushed a commit to supnewmobile/pdf_viewer that referenced this issue Apr 22, 2022
lohanidamodar pushed a commit that referenced this issue May 4, 2022
Co-authored-by: Nicodème Stalder <[email protected]>
@ErfanHz
Copy link

ErfanHz commented Aug 29, 2022

Thanks. fixed !!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants