gpt4 book ai didi

android - 为什么 PDFViewer 在使用 Barteksc PDFViewer 打开文件时总是给出 NullPointer 异常

转载 作者:行者123 更新时间:2023-11-30 00:14:52 25 4
gpt4 key购买 nike

我有一个文件要从 Assets 加载并在应用程序中显示 pdf。我在 Assets 文件夹中显示了文件“one.pdf”。

我的依赖项是:

    compile 'com.github.barteksc:android-pdf-viewer:2.8.1'
compile "commons-io:commons-io:+"

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

    pdfView= (PDFView)findViewById(R.id.pdfView);
setContentView(R.layout.activity_main);
pdfView.fromAsset("one.pdf").load();

显示PDF的代码(DisplayActivity.xml)

    <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"/>

错误日志:

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) 

该文件存在于 Assets 文件夹中。我在 Manifest 中添加了访问外部存储的权限。出现以下错误的原因可能是什么?

最佳答案

改变你的依赖到

compile 'com.github.barteksc:android-pdf-viewer:2.8.1'
compile "commons-io:commons-io:+"

来自

compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
compile 'org.apache.commons:commons-collections4:4.1'

那么你的Xml文件应该有

<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"/>

以及您的 Activity 代码:

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 + "-");
}
}
}

}

同时添加此权限

 <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

并确保您的 minSdkVersion 15 或更高。

它将正常工作。

关于android - 为什么 PDFViewer 在使用 Barteksc PDFViewer 打开文件时总是给出 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47450564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com