首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么PDFViewer总是在使用Barteksc PDFViewer打开文件时提供NullPointer异常

为什么PDFViewer总是在使用Barteksc PDFViewer打开文件时提供NullPointer异常
EN

Stack Overflow用户
提问于 2017-11-23 08:16:52
回答 1查看 1.8K关注 0票数 1

我有一个文件从资产加载和显示的pdf在应用程序。我在资产文件夹中显示了"one.pdf“文件。

我的依赖关系是:

代码语言:javascript
复制
    compile 'com.github.barteksc:android-pdf-viewer:2.8.1'
    compile "commons-io:commons-io:+"

在屏幕上加载PDF的代码是(DisplayActivity.java):

代码语言:javascript
复制
    pdfView= (PDFView)findViewById(R.id.pdfView);
    setContentView(R.layout.activity_main);
    pdfView.fromAsset("one.pdf").load();

显示PDF的代码(DisplayActivity.xml)

代码语言:javascript
复制
    <com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_below="@+id/tv_header"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

错误日志:

代码语言:javascript
复制
FATAL EXCEPTION: main Process: in.edu.reva.revauniversity_pu, PID: 15597
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.edu.reva.revauniversity_pu/in.edu.reva.revauniversity_pu.FormulaActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromAsset(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2720)
at android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                   
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1466)
at android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   
at android.os.Looper.loop(Looper.java:154)                                                                                   
at android.app.ActivityThread.main(ActivityThread.java:6111)                                                                                   
at java.lang.reflect.Method.invoke(Native Method)                                                                                   
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromAsset(java.lang.String)' on a null object reference
at in.edu.reva.revauniversity_pu.FormulaActivity.onCreate(FormulaActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6734)                                                                                   
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2720  
at android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                    
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1466)
at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6111)                                                                                    
at java.lang.reflect.Method.invoke(Native Method)                                                                                    
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

该文件存在于“资产”文件夹中。我添加了访问Manifest中外部存储的权限。以下错误的原因是什么?

EN

回答 1

Stack Overflow用户

发布于 2017-11-23 08:34:01

将依赖项更改为

代码语言:javascript
复制
compile 'com.github.barteksc:android-pdf-viewer:2.8.1'
compile "commons-io:commons-io:+"

从…

代码语言:javascript
复制
compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
compile 'org.apache.commons:commons-collections4:4.1'

那么Xml文件应该有

代码语言:javascript
复制
<com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_below="@+id/tv_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

你的活动守则:

代码语言:javascript
复制
public class YourActivity extends AppCompatActivity implements OnPageChangeListener, OnLoadCompleteListener {
    public static final String SAMPLE_FILE = "one.pdf";

    PDFView pdfView;
    Integer pageNumber = 0;
    String pdfFileName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your);
        pdfView = (PDFView) findViewById(R.id.pdfView);
        displayFromAsset(SAMPLE_FILE);
    }

    private void displayFromAsset(String assetFileName) {
        pdfFileName = assetFileName;
        pdfView.fromAsset(SAMPLE_FILE).defaultPage(pageNumber).enableSwipe(true)
                .swipeHorizontal(false)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(this))
                .load();
    }


    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
        setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
    }


    @Override
    public void loadComplete(int nbPages) {
        PdfDocument.Meta meta = pdfView.getDocumentMeta();
        printBookmarksTree(pdfView.getTableOfContents(), "-");

    }

    public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
        for (PdfDocument.Bookmark b : tree) {

            Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

            if (b.hasChildren()) {
                printBookmarksTree(b.getChildren(), sep + "-");
            }
        }
    }

}

还添加此权限

代码语言:javascript
复制
 <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

MAke确定您的minSdkVersion 15或更高。

会很好的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47450564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档