尝试在 AndroidPdfViewer 中调用虚方法崩溃
Attempt to invoke virtual method crash in AndroidPdfViewer
使用 AndroidPdfViewer 包。
我的 build.gradle 代码:
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'
}
Activity XML 文件:
<?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>
Activity代码:
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
运行 错误:
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)
采取的解决步骤:
在 @Override
和 public class activity_fact_sheet extends AppCompatActivity {
之上的 activity 代码中添加了 PDFView pdfView;
还检查了我的 activity 打开方法是否有问题(不是)。
似乎错误是在 activity 代码中的 pdfView.fromAsset("file2.pdf").load();
上引发的。这让我觉得它无法在资产中找到 file2.pdf,但我可能错了。
当您尝试打开 activity 时应用程序崩溃。
尝试使用 this 视频实现它。
好的,我在 stackexchange 上找到了另一个主题(除了无数关于空指针异常的主题 - 这对我没有帮助)。 This 有有效的代码。只需导入
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 的 activity 中。将 public static final string SAMPLE_FILE
更改为您要显示的实际 pdf(在您的资产文件夹中)。
如果您在 Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));
中的 TAG
上遇到错误,您必须在 public static final String SAMPLE_FILE = "yourfilehere.pdf";
下添加 private static final String TAG = "youractivitynamehere";
就是这样!希望搜索此问题的任何其他人都能找到这个。
将此代码添加到您的 XML 文件中
<com.github.barteksc.pdfviewer.PDFView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pdfView"
></com.github.barteksc.pdfviewer.PDFView>
使用 AndroidPdfViewer 包。 我的 build.gradle 代码:
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'
}
Activity XML 文件:
<?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>
Activity代码:
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
运行 错误:
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)
采取的解决步骤:
在 @Override
和 public class activity_fact_sheet extends AppCompatActivity {
之上的 activity 代码中添加了 PDFView pdfView;
还检查了我的 activity 打开方法是否有问题(不是)。
似乎错误是在 activity 代码中的 pdfView.fromAsset("file2.pdf").load();
上引发的。这让我觉得它无法在资产中找到 file2.pdf,但我可能错了。
当您尝试打开 activity 时应用程序崩溃。
尝试使用 this 视频实现它。
好的,我在 stackexchange 上找到了另一个主题(除了无数关于空指针异常的主题 - 这对我没有帮助)。 This 有有效的代码。只需导入
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 的 activity 中。将 public static final string SAMPLE_FILE
更改为您要显示的实际 pdf(在您的资产文件夹中)。
如果您在 Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));
中的 TAG
上遇到错误,您必须在 public static final String SAMPLE_FILE = "yourfilehere.pdf";
private static final String TAG = "youractivitynamehere";
就是这样!希望搜索此问题的任何其他人都能找到这个。
将此代码添加到您的 XML 文件中
<com.github.barteksc.pdfviewer.PDFView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pdfView"
></com.github.barteksc.pdfviewer.PDFView>