You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android文档分享应用搜索时PDFViewer空指针崩溃求助

问题描述

开发文档分享应用时,首页使用PDFViewer可正常运行,但通过搜索功能打开文档时应用崩溃。

错误日志

---------------------------- PROCESS STARTED (3025) for package com.example.edventure ----------------------------
2023-03-27 19:51:23.789  3025-3025  AndroidRuntime          com.example.edventure                E  FATAL EXCEPTION: main
                                                                                                    Process: com.example.edventure, PID: 3025
                                                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.HandlerThread.isAlive()' on a null object reference
                                                                                                        at com.github.barteksc.pdfviewer.PDFView.loadComplete(PDFView.java:756)
                                                                                                        at com.github.barteksc.pdfviewer.DecodingAsyncTask.onPostExecute(DecodingAsyncTask.java:80)
                                                                                                        at com.github.barteksc.pdfviewer.DecodingAsyncTask.onPostExecute(DecodingAsyncTask.java:27)
                                                                                                        at android.os.AsyncTask.finish(AsyncTask.java:771)
                                                                                                        at android.os.AsyncTask.access$900(AsyncTask.java:199)
                                                                                                        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loop(Looper.java:263)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8296)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)

疑似出错代码

public static void loadPdfFromUrlSinglePage(String pdfUrl, String pdfTitle, PDFView pdfView, ProgressBar progressBar) {
        String TAG = "PDF_LOAD_SINGLE_TAG";
        StorageReference ref = FirebaseStorage.getInstance().getReferenceFromUrl(pdfUrl);
        ref.getBytes(MAX_BYTES_PDF)
                .addOnSuccessListener(new OnSuccessListener<byte[]>() {
                    @Override
                    public void onSuccess(byte[] bytes) {
                        Log.d(TAG,"onSuccess: " + pdfTitle + "successfully got the file");

                        //set to pdfview
                        pdfView.fromBytes(bytes)
                                .pages(0)
                                .spacing(0)
                                .swipeHorizontal(false)
                                .enableSwipe(false)
                                .onError(new OnErrorListener() {
                                    @Override
                                    public void onError(Throwable t) {
                                        progressBar.setVisibility(View.INVISIBLE);
                                        Log.d(TAG,"onError: "+t.getMessage());
                                    }
                                })
                                .onPageError(new OnPageErrorListener() {
                                    @Override
                                    public void onPageError(int page, Throwable t) {
                                        progressBar.setVisibility(View.INVISIBLE);
                                        Log.d(TAG,"onPageError: "+t.getMessage());
                                    }
                                })
                                .onLoad(new OnLoadCompleteListener() {
                                    @Override
                                    public void loadComplete(int nbPages) {
                                        progressBar.setVisibility(View.INVISIBLE);
                                        Log.d(TAG,"loadComplete: pdf loaded");
                                    }
                                })
                                .load();
                    }
                })

解决方案

1. 提前检查View实例有效性

搜索场景下,PDF异步加载任务完成时,承载View的页面可能已经销毁,导致PDFView/ProgressBar实例被回收触发空指针。需在回调执行前添加状态判断:

@Override
public void onSuccess(byte[] bytes) {
    Log.d(TAG,"onSuccess: " + pdfTitle + "successfully got the file");

    // 检查PDFView是否仍有效
    if (pdfView == null || !pdfView.isAttachedToWindow()) {
        if (progressBar != null && progressBar.isAttachedToWindow()) {
            progressBar.setVisibility(View.INVISIBLE);
        }
        Log.d(TAG,"onSuccess: PDFView is invalid, aborting load");
        return;
    }

    pdfView.fromBytes(bytes)
            .pages(0)
            .spacing(0)
            .swipeHorizontal(false)
            .enableSwipe(false)
            .onError(new OnErrorListener() {
                @Override
                public void onError(Throwable t) {
                    if (progressBar != null && progressBar.isAttachedToWindow()) {
                        progressBar.setVisibility(View.INVISIBLE);
                    }
                    Log.d(TAG,"onError: "+t.getMessage());
                }
            })
            .onPageError(new OnPageErrorListener() {
                @Override
                public void onPageError(int page, Throwable t) {
                    if (progressBar != null && progressBar.isAttachedToWindow()) {
                        progressBar.setVisibility(View.INVISIBLE);
                    }
                    Log.d(TAG,"onPageError: "+t.getMessage());
                }
            })
            .onLoad(new OnLoadCompleteListener() {
                @Override
                public void loadComplete(int nbPages) {
                    if (progressBar != null && progressBar.isAttachedToWindow()) {
                        progressBar.setVisibility(View.INVISIBLE);
                    }
                    Log.d(TAG,"loadComplete: pdf loaded");
                }
            })
            .load();
}

2. 页面销毁时清理PDFView资源

在承载PDFView的Activity/Fragment的onDestroy方法中,主动清理资源,避免异步任务回调访问已销毁实例:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (pdfView != null) {
        pdfView.recycle();
        pdfView.clear();
    }
}

3. 取消静态方法持有View引用

当前加载方法为静态,传入View实例易引发内存泄漏且无法感知生命周期。建议改为非静态方法,绑定到页面组件中。

4. 升级AndroidPdfViewer版本

确认使用的版本是否包含线程管理相关修复,尝试升级到最新稳定版,规避已知的空指针bug。

内容的提问来源于stack exchange,提问作者Devansh Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 21:32:43