From 422c014ee74277e45a502468e031c150a52d09fb Mon Sep 17 00:00:00 2001 From: Ahmer Afzal Date: Mon, 14 Feb 2022 15:51:31 +0500 Subject: [PATCH] Fix a potential NPE Crash due to Mismatched Thread Lifecycle Fix a potential NPE due to out of sync render thread management [Pull Request](https://github.com/barteksc/AndroidPdfViewer/pull/824) Update the sample app configuration so that it can compile [Pull Request](https://github.com/mhiew/AndroidPdfViewer/pull/2) --- PdfViewer/src/main/java/com/ahmer/pdfviewer/PDFView.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/PdfViewer/src/main/java/com/ahmer/pdfviewer/PDFView.kt b/PdfViewer/src/main/java/com/ahmer/pdfviewer/PDFView.kt index 06467b0..33a034d 100644 --- a/PdfViewer/src/main/java/com/ahmer/pdfviewer/PDFView.kt +++ b/PdfViewer/src/main/java/com/ahmer/pdfviewer/PDFView.kt @@ -519,6 +519,7 @@ class PDFView(context: Context?, set: AttributeSet?) : RelativeLayout(context, s fun loadComplete(pdfFile: PdfFile) { mState = State.LOADED this.pdfFile = pdfFile + if (mRenderingHandlerThread == null) return if (mRenderingHandlerThread?.isAlive != true) mRenderingHandlerThread?.start() renderingHandler = mRenderingHandlerThread?.looper?.let { RenderingHandler(it, this) @@ -973,6 +974,11 @@ class PDFView(context: Context?, set: AttributeSet?) : RelativeLayout(context, s mAnimationManager?.computeFling() } + override fun onAttachedToWindow() { + super.onAttachedToWindow() + if (mRenderingHandlerThread == null) mRenderingHandlerThread = HandlerThread("PDF renderer") + } + override fun onDetachedFromWindow() { recycle() if (mRenderingHandlerThread != null) { @@ -1325,7 +1331,6 @@ class PDFView(context: Context?, set: AttributeSet?) : RelativeLayout(context, s } private fun initPDFView() { - mRenderingHandlerThread = HandlerThread("PDF renderer") if (isInEditMode) return mPdfiumCore = PdfiumCore(context) cacheManager = CacheManager()