Android PDF Viewer空对象引用错误

时间:2018-09-21 19:26:31

标签: java android android-studio android-activity reference

我和其他许多人一样,正在使用barteksc Android PDF Viewer(https://github.com/barteksc/AndroidPdfViewer)。我不断收到空对象引用错误。这是错误。

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: group6.interactivehandwriting, PID: 24037
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{group6.interactivehandwriting/group6.interactivehandwriting.activities.RoomActivity.RoomActivity}: android.view.InflateException: Binary XML file line #9: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

我已检查以确保正确引用了视图。我正在加载的资产“ sample.pdf”在我的资产中。

*您能看到我收到此错误的任何原因吗? *

我意识到关于该库的文章很多,但是每个实例似乎都是唯一的,所以我决定继续询问。

RoomActivity.java

package group6.interactivehandwriting.activities.RoomActivity;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;

import group6.interactivehandwriting.R;
import group6.interactivehandwriting.activities.RoomActivity.views.DocumentView;
import group6.interactivehandwriting.activities.RoomActivity.views.RoomView;

public class RoomActivity extends Activity implements OnPageChangeListener, OnLoadCompleteListener {
    RoomView roomView;
    DocumentView docView;
    PDFView pView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("Android: ", "Setting content view to an instance of RoomView in: " + this.getClass().getName());
        setContentView(R.layout.activity_draw);

        System.out.println("Attempting to set ImageView..");
        pView = (PDFView) findViewById(R.id.pdfView);
        pView.fromAsset("sample.pdf").defaultPage(0)
                .enableSwipe(true).swipeHorizontal(false).
                onPageChange(this).enableAnnotationRendering(true).onLoad(this).scrollHandle(new DefaultScrollHandle(this)).load();


    }

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

    @Override
    public void loadComplete(int nbPages) {
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="group6.interactivehandwriting">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activities.RoomActivity.RoomActivity"
            android:label="@string/title_activity_menu"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_draw.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.RoomActivity.RoomActivity">

    <view
        android:id="@+id/room_view"
        id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/room"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

依赖项:

compile 'com.github.barteksc:android-pdf-viewer:2.8.2'

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

1 个答案:

答案 0 :(得分:1)

您的activity_draw.xml中有一个错字!

应该为View,而不是view 在第9行。

也删除以下行-

id="@+id/view