首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在AndroidPdfViewer中调用虚拟方法crash

尝试在AndroidPdfViewer中调用虚拟方法crash
EN

Stack Overflow用户
提问于 2018-01-18 19:16:35
回答 2查看 1.8K关注 0票数 0

使用AndroidPdfViewer包。我的build.gradle代码:

代码语言:javascript
复制
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation("com.github.bumptech.glide:glide:4.5.0") {
    exclude group: "com.android.support"
}
implementation "com.android.support:support-fragment:26.1.0"
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'

}

活动XML文件:

代码语言:javascript
复制
    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

活动代码:

代码语言:javascript
复制
package eu.myapp.myapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.barteksc.pdfviewer.PDFView;

public class activity_fact_sheet extends AppCompatActivity {

    PDFView pdfView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PDFView pdfView = (PDFView) findViewById(R.id.pdfView);
        pdfView.fromAsset("file2.pdf").load();
    }
}

file2.pdf位于app > src > main > assets中

运行时出错:

代码语言:javascript
复制
01-18 11:59:25.475 29674-29674/eu.myapp.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: eu.myapp.myapp, PID: 29674
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.myapp.myapp/eu.myapp.myapp.activity_fact_sheet}: 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:2924)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
                                                                            at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                         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 eu.myapp.myapp.activity_fact_sheet.onCreate(activity_fact_sheet.java:17)
                                                                            at android.app.Activity.performCreate(Activity.java:6912)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) 
                                                                            at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:154) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6692) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

解决该问题所采取的步骤:

@Overridepublic class activity_fact_sheet extends AppCompatActivity {下的activity代码中添加了PDFView pdfView;,同时也检查了我的activity打开方法是否有问题(不是)。

这个错误似乎是在活动代码的pdfView.fromAsset("file2.pdf").load();上抛出的。这使我觉得它在资产中找不到file2.pdf,但我可能错了。

当您尝试打开活动时,应用程序崩溃。

我尝试使用this视频来实现它。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-19 16:43:44

好吧,我在stackexchange上发现了另一个话题(除了无数关于空指针异常的话题--这对我没有帮助)。This提供了可以工作的代码。只需导入

代码语言:javascript
复制
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'

compile 'org.apache.commons:commons-collections4:4.1'

在gradle构建文件中。

然后确保您在AndroidManifest.xml中拥有<uses-permission android:name="android.permission.WRITE_SETTINGS"/>权限。(不管怎样,你应该已经有这个了。)

然后确保将他的代码复制到您想要显示PDF的活动中。将public static final string SAMPLE_FILE更改为您希望显示的实际pdf (在您的assets文件夹中)。

如果在Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));中的TAG上出现错误,则必须将private static final String TAG = "youractivitynamehere";添加到public static final String SAMPLE_FILE = "yourfilehere.pdf";下面

就这样!希望任何其他搜索这个问题的人都能找到这个。

票数 0
EN

Stack Overflow用户

发布于 2021-07-17 19:30:44

将此代码添加到XML文件中

代码语言:javascript
复制
<com.github.barteksc.pdfviewer.PDFView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pdfView"
></com.github.barteksc.pdfviewer.PDFView>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48320013

复制
相关文章

相似问题

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