我有一个文件从资产加载和显示的pdf在应用程序。我在资产文件夹中显示了"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) 该文件存在于“资产”文件夹中。我添加了访问Manifest中外部存储的权限。以下错误的原因是什么?
发布于 2017-11-23 08:34:01
将依赖项更改为
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"/>你的活动守则:
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"/>MAke确定您的minSdkVersion 15或更高。
会很好的。
https://stackoverflow.com/questions/47450564
复制相似问题