Android Studio - PDFRendererBasic - 无法替换sample.pdf?

时间:2016-02-11 17:04:27

标签: android pdf pdfrenderer

每次我编辑PDFRendererBasicFragment java代码

mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();

到另一个放在assets文件夹中的pdf,应用程序崩溃..

Logcat报告:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.pdfrendererbasic/com.example.android.pdfrendererbasic.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference

为什么会这样?我甚至尝试在构建之前将我的PDF重命名为sample.pdf并且它仍然崩溃。原始代码的一切正常,只是不让我替换PDF。

堆栈追踪:

02-11 11:11:50.130 18201-18201/com.example.android.pdfrendererbasic I/art: Not late-enabling -Xcheck:jni (already on)
02-11 11:11:50.130 18201-18201/com.example.android.pdfrendererbasic I/art: Late-enabling JIT
02-11 11:11:50.132 18201-18201/com.example.android.pdfrendererbasic I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
02-11 11:11:50.201 18201-18201/com.example.android.pdfrendererbasic W/System: ClassLoader referenced unknown path: /data/app/com.example.android.pdfrendererbasic-1/lib/x86
02-11 11:11:50.252 18201-18201/com.example.android.pdfrendererbasic W/System.err: java.io.IOException: cannot create document. Error: 3
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.graphics.pdf.PdfRenderer.<init>(PdfRenderer.java:153)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.openRenderer(PdfRendererBasicFragment.java:141)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.onAttach(PdfRendererBasicFragment.java:108)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.Fragment.onAttach(Fragment.java:1380)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:932)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.BackStackRecord.run(BackStackRecord.java:793)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.FragmentController.execPendingActions(FragmentController.java:325)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.Activity.performStart(Activity.java:6252)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.os.Looper.loop(Looper.java:148)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
02-11 11:11:50.254 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-11 11:11:51.082 18201-18201/com.example.android.pdfrendererbasic D/AndroidRuntime: Shutting down VM
02-11 11:11:51.082 18201-18201/com.example.android.pdfrendererbasic E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.android.pdfrendererbasic, PID: 18201
                                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.pdfrendererbasic/com.example.android.pdfrendererbasic.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference
                                                                                          at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.showPage(PdfRendererBasicFragment.java:163)
                                                                                          at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.onViewCreated(PdfRendererBasicFragment.java:101)
                                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:988)
                                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                                          at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                                          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
                                                                                          at android.app.FragmentController.execPendingActions(FragmentController.java:325)
                                                                                          at android.app.Activity.performStart(Activity.java:6252)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                          at android.os.Looper.loop(Looper.java:148) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

PDFRendererBasicFragment.java:

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.pdfrendererbasic;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;

/**
 * This fragment has a big {@ImageView} that shows PDF pages, and 2 {@link android.widget.Button}s to move between
 * pages. We use a {@link android.graphics.pdf.PdfRenderer} to render PDF pages as {@link android.graphics.Bitmap}s.
 */
public class PdfRendererBasicFragment extends Fragment implements View.OnClickListener {

    /**
     * Key string for saving the state of current page index.
     */
    private static final String STATE_CURRENT_PAGE_INDEX = "current_page_index";

    /**
     * File descriptor of the PDF.
     */
    private ParcelFileDescriptor mFileDescriptor;

    /**
     * {@link android.graphics.pdf.PdfRenderer} to render the PDF.
     */
    private PdfRenderer mPdfRenderer;

    /**
     * Page that is currently shown on the screen.
     */
    private PdfRenderer.Page mCurrentPage;

    /**
     * {@link android.widget.ImageView} that shows a PDF page as a {@link android.graphics.Bitmap}
     */
    private ImageView mImageView;

    /**
     * {@link android.widget.Button} to move to the previous page.
     */
    private Button mButtonPrevious;

    /**
     * {@link android.widget.Button} to move to the next page.
     */
    private Button mButtonNext;

    public PdfRendererBasicFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pdf_renderer_basic, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // Retain view references.
        mImageView = (ImageView) view.findViewById(R.id.image);
        mButtonPrevious = (Button) view.findViewById(R.id.previous);
        mButtonNext = (Button) view.findViewById(R.id.next);
        // Bind events.
        mButtonPrevious.setOnClickListener(this);
        mButtonNext.setOnClickListener(this);
        // Show the first page by default.
        int index = 0;
        // If there is a savedInstanceState (screen orientations, etc.), we restore the page index.
        if (null != savedInstanceState) {
            index = savedInstanceState.getInt(STATE_CURRENT_PAGE_INDEX, 0);
        }
        showPage(index);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            openRenderer(activity);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(activity, "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
            activity.finish();
        }
    }

    @Override
    public void onDetach() {
        try {
            closeRenderer();
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.onDetach();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (null != mCurrentPage) {
            outState.putInt(STATE_CURRENT_PAGE_INDEX, mCurrentPage.getIndex());
        }
    }

    /**
     * Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources.
     */
    private void openRenderer(Context context) throws IOException {
        // In this sample, we read a PDF from the assets directory.
        mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();
        // This is the PdfRenderer we use to render the PDF.
        mPdfRenderer = new PdfRenderer(mFileDescriptor);
    }

    /**
     * Closes the {@link android.graphics.pdf.PdfRenderer} and related resources.
     *
     * @throws java.io.IOException When the PDF file cannot be closed.
     */
    private void closeRenderer() throws IOException {
        if (null != mCurrentPage) {
            mCurrentPage.close();
        }
        mPdfRenderer.close();
        mFileDescriptor.close();
    }

    /**
     * Shows the specified page of PDF to the screen.
     *
     * @param index The page index.
     */
    private void showPage(int index) {
        if (mPdfRenderer.getPageCount() <= index) {
            return;
        }
        // Make sure to close the current page before opening another one.
        if (null != mCurrentPage) {
            mCurrentPage.close();
        }
        // Use `openPage` to open a specific page in PDF.
        mCurrentPage = mPdfRenderer.openPage(index);
        // Important: the destination bitmap must be ARGB (not RGB).
        Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(),
                Bitmap.Config.ARGB_8888);
        // Here, we render the page onto the Bitmap.
        // To render a portion of the page, use the second and third parameter. Pass nulls to get
        // the default result.
        // Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter.
        mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
        // We are ready to show the Bitmap to user.
        mImageView.setImageBitmap(bitmap);
        updateUi();
    }

    /**
     * Updates the state of 2 control buttons in response to the current page index.
     */
    private void updateUi() {
        int index = mCurrentPage.getIndex();
        int pageCount = mPdfRenderer.getPageCount();
        mButtonPrevious.setEnabled(0 != index);
        mButtonNext.setEnabled(index + 1 < pageCount);
        getActivity().setTitle(getString(R.string.app_name_with_index, index + 1, pageCount));
    }

    /**
     * Gets the number of pages in the PDF. This method is marked as public for testing.
     *
     * @return The number of pages.
     */
    public int getPageCount() {
        return mPdfRenderer.getPageCount();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.previous: {
                // Move to the previous page
                showPage(mCurrentPage.getIndex() - 1);
                break;
            }
            case R.id.next: {
                // Move to the next page
                showPage(mCurrentPage.getIndex() + 1);
                break;
            }
        }
    }

}

2 个答案:

答案 0 :(得分:2)

将gradle版本从2.2.0降级到2.1.2并且崩溃将会消失。

在build.gradle(Project)中替换

classpath 'com.android.tools.build:gradle:2.2.0'

classpath 'com.android.tools.build:gradle:2.1.2'

答案 1 :(得分:1)

您提到的同一问题已报告并描述here。问题似乎与Android Studio创建最终apk文件的方式有关。虽然在给定的链接中提供了手动重新打包apk文件的解决方案,但这不是很实用。该错误似乎没有任何其他简单的解决方案。

修改

以下是所提供链接的解决方案。但是,报告的问题提到了稍有不同的错误消息,因此可能无效。

  1. 启动Android Studio
  2. 点击导入Android代码示例
  3. 选择图形&gt; Pdf Renderer Basic
  4. 点击下一步
  5. 单击“完成”。
  6. 点击“运行”&gt;运行'应用程序'
  7. 运行应用程序隐式创建Application / build / outputs / apk / Application-debug.apk文件。

    6.a:完整性检查:验证META-INF条目位于文件末尾:

    $ unzip -lv Application/build/outputs/apk/Application-debug.apk
    ...
        1569  Defl:N      702  55%  12-16-14 17:35  c7cb7a03  META-INF/MANIFEST.MF
        1598  Defl:N      724  55%  12-16-14 17:35  034c0dfc  META-INF/CERT.SF
         776  Defl:N      605  22%  12-16-14 17:35  2be8b27c  META-INF/CERT.RSA
    --------          -------  ---                            -------
     1565085           849785  46%                            21 files
    
    1. 使用jarsigner手动重新签名包。这样做的目的是确保META-INF条目位于.apk的开头:

      $ yes android | keytool -genkey -v -keystore demo.keystore -alias demo -keyalg RSA -keysize 2048 -validity 10000 -dname“CN = Mark Smith” $ cp Application / build / outputs / apk / Application-debug.apk A.apk $ zip -d A.apk META-INF / MANIFEST.MF $ zip -d A.apk META-INF / CERT.SF $ zip -d A.apk META-INF / CERT.RSA $ jarsigner -keystore demo.keystore -storepass android \     -keypass android -digestalg SHA1 -sigalg md5withRSA \     -signedjar A-unaligned.apk A.apk \     演示 $ zipalign 4 A-unaligned.apk A-aligned.apk

    2. 安装重新签名的软件包:

      $ adb uninstall com.example.android.pdfrendererbasic $ adb install A-aligned.apk

    3. 运行新包:

      $ adb shell am start com.example.android.pdfrendererbasic / .MainActivity