Skip to content

Commit 41f912e

Browse files
committed
Fix a potential NPE Crash due to Mismatched Thread Lifecycle
- the thread creation and recycling was not being handled symmetrically - this fix is based off of these two PR: DImuthuUpe#824 and #2
1 parent 81ae29e commit 41f912e

File tree

1 file changed

+12
-2
lines changed
  • android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer

1 file changed

+12
-2
lines changed

android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/PDFView.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ ScrollHandle getScrollHandle() {
245245
public PDFView(Context context, AttributeSet set) {
246246
super(context, set);
247247

248-
renderingHandlerThread = new HandlerThread("PDF renderer");
249-
250248
if (isInEditMode()) {
251249
return;
252250
}
@@ -462,6 +460,14 @@ public void computeScroll() {
462460
animationManager.computeFling();
463461
}
464462

463+
@Override
464+
protected void onAttachedToWindow() {
465+
super.onAttachedToWindow();
466+
if (renderingHandlerThread == null) {
467+
renderingHandlerThread = new HandlerThread("PDF renderer");
468+
}
469+
}
470+
465471
@Override
466472
protected void onDetachedFromWindow() {
467473
recycle();
@@ -753,6 +759,10 @@ void loadComplete(PdfFile pdfFile) {
753759

754760
this.pdfFile = pdfFile;
755761

762+
if (renderingHandlerThread == null) {
763+
return;
764+
}
765+
756766
if (!renderingHandlerThread.isAlive()) {
757767
renderingHandlerThread.start();
758768
}

0 commit comments

Comments
 (0)