【问题标题】:error activity close after creating pdf file创建 pdf 文件后关闭错误活动
【发布时间】:2017-06-09 11:14:39
【问题描述】:

我正在尝试生成 pdf。我也成功生成了 pdf,但我的应用程序在创建 pdf 后关闭。我搜索但没有找到任何答案。我按照这个链接 Creating a pdf file in android programmatically and writing in it 我不想关闭我的应用程序并在 pdfviewer 中查看 pdf 这是我的代码

    private void createPDF(ArrayList<Searchresult> alllist)  {

            com.itextpdf.text.Document doc= new com.itextpdf.text.Document(PageSize.A4,50,50,50,50);
            try {
                String USER_PASS = "hasu123";
                String OWNER_PASS = "vnr123";
                 String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir";

            File dir = new File(path);
            if(!dir.exists())
                dir.mkdirs();

            File file = new File(dir, "newFile.pdf");
                FileOutputStream fOut = new FileOutputStream(file);
                PdfWriter writer=PdfWriter.getInstance(doc, fOut);

              /*  writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
                        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);*/
                HeaderAndFooter event = new HeaderAndFooter();
                writer.setPageEvent(event);

                //open the document
                doc.open();
               /* PdfReader reader = new PdfReader(path);
                PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(path));
                stamper.setEncryption(USER_PASS.getBytes(),USER_PASS.getBytes(),
                        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
                stamper.close();
                reader.close();*/

                PdfPTable table = new PdfPTable(8);
                Font subtitleFont = FontFactory.getFont("Times Roman",11, BaseColor.BLUE);
                Font subtitle = FontFactory.getFont("Times Roman",7, BaseColor.BLACK);

                PdfPCell pcell = new PdfPCell(new Phrase("Stoneid",subtitleFont));
                pcell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell);
                PdfPCell pcell2 = new PdfPCell(new Phrase("Shape",subtitleFont));
                pcell2.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell2);
                PdfPCell pcell3 = new PdfPCell(new Phrase("Carat",subtitleFont));
                pcell3.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell3);
                PdfPCell pcell4 = new PdfPCell(new Phrase("Color",subtitleFont));
                pcell4.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell4);
                PdfPCell pcell5 = new PdfPCell(new Phrase("Clarity",subtitleFont));
                pcell5.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell5);
                PdfPCell pcell6 = new PdfPCell(new Phrase("Cut",subtitleFont));
                pcell6.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell6);
                PdfPCell pcell7 = new PdfPCell(new Phrase("Polish",subtitleFont));
                pcell7.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell7);
                PdfPCell pcell8 = new PdfPCell(new Phrase("Symm",subtitleFont));
                pcell8.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell8);
                table.setHeaderRows(1);

                for (int i=0;i<alllist.size();i++){
                    String stoneid=alllist.get(i).getStoneid();
                    String shape=alllist.get(i).getShape();
                    String carat=alllist.get(i).getSize();
                    String color=alllist.get(i).getColor();
                    String cut=alllist.get(i).getCut();
                    String polish=alllist.get(i).getPolish();
                    String symmm=alllist.get(i).getSym();
                    String clarity=alllist.get(i).getClarity();
                    if(color.equalsIgnoreCase("")){
                        color="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(stoneid.equalsIgnoreCase("")){
                        stoneid="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(shape.equalsIgnoreCase("")){
                        shape="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(carat.equalsIgnoreCase("")){
                        carat="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(color.equalsIgnoreCase("")){
                        color="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(cut.equalsIgnoreCase("")){
                        cut="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(symmm.equalsIgnoreCase("")){
                        symmm="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(polish.equalsIgnoreCase("")){
                        polish="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(clarity.equalsIgnoreCase("")){
                        clarity="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    table.addCell(createCel(stoneid,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(shape,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(carat,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(color,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(cut,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(symmm,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(polish,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(clarity,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                }
                doc.add(table);
            } catch (DocumentException de) {
                Log.e("PDFCreator", "DocumentException:" + de);
            } catch (IOException e) {
                Log.e("PDFCreator", "ioException:" + e);
            }
            finally {
                doc.close();
            }
           viewPdf("newFile.pdf", "Dir");
        }


 private PdfPCell createCel(String s, Font subtitle, int alignCenter) {
        PdfPCell pdfPCel = new PdfPCell(new Phrase(s,subtitle));
        pdfPCel.setHorizontalAlignment(alignCenter);
        return pdfPCel;
    }


    private void viewPdf(String s, final String dir) {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
                runOnUiThread(new Runnable() {
                    public void run() {
                        File file = new File(Environment.getExternalStorageDirectory() + "/" + dir + "/" + "newFile.pdf");
                        Intent target = new Intent(Intent.ACTION_VIEW);
                        //target.setPackage("com.adobe.reader");
                        target.setDataAndType(Uri.fromFile(file),"application/pdf");
                        target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        Intent intent = Intent.createChooser(target, "Open File");

                        try {
                            startActivity(intent);
                        } catch (ActivityNotFoundException e) {
                            // Instruct the user to install a PDF reader here, or something
                            Toast.makeText(ResultDataNew.this, "No PDF Viewer is install", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }.start();
    }

这个日志猫:

06-09 07:21:37.993 1334-1334/? E/libprocessgroup: failed to make and chown /acct/uid_10011: Read-only file system
06-09 07:21:37.993 1334-1334/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
06-09 07:21:37.993 1334-1334/? I/art: Late-enabling -Xcheck:jni
06-09 07:21:38.058 1334-1334/Abc.com W/System: ClassLoader referenced unknown path: /data/app/Abc.com-1/lib/x86
06-09 07:21:38.231 1334-1353/Abc.com D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                              [ 06-09 07:21:38.236  1334: 1334 D/         ]
                                                              HostConnection::get() New Host Connection established 0xe9b1eb90, tid 1334
06-09 07:21:38.296 1334-1353/Abc.com D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
06-09 07:21:38.297 1334-1353/Abc.com D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-09 07:21:38.310 1334-1353/Abc.com D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so

                                                      [ 06-09 07:21:38.329  1334: 1353 D/         ]
                                                      HostConnection::get() New Host Connection established 0xee952bc0, tid 1353
06-09 07:21:38.388 1334-1353/Abc.com I/OpenGLRenderer: Initialized EGL, version 1.4
06-09 07:21:38.464 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:38.464 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xee9565a0, error=EGL_SUCCESS
06-09 07:21:42.143 1334-1334/Abc.com I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-09 07:21:42.149 1334-1334/Abc.com I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-09 07:21:42.225 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:42.225 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xee956640, error=EGL_SUCCESS
06-09 07:21:42.633 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2150
06-09 07:21:52.584 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:52.584 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe9b3d840, error=EGL_SUCCESS
06-09 07:21:53.100 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:21:53.107 1334-1353/Abc.com D/OpenGLRenderer: endAllStagingAnimators on 0xde792000 (RippleDrawable) with handle 0xee952da0
06-09 07:21:54.178 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:54.178 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xee9335c0, error=EGL_SUCCESS
06-09 07:21:54.885 1334-1334/Abc.com I/Choreographer: Skipped 39 frames!  The application may be doing too much work on its main thread.
06-09 07:21:55.172 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe9ba5920
06-09 07:21:56.128 1334-1334/Abc.com E/shape: HEART,
06-09 07:21:57.511 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:57.511 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xde4491a0, error=EGL_SUCCESS
06-09 07:21:58.203 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:22:02.868 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:02.868 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe8d61480, error=EGL_SUCCESS
06-09 07:22:02.927 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:02.927 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe8d613a0, error=EGL_SUCCESS
06-09 07:22:02.946 1334-1334/Abc.com E/RecyclerView: No adapter attached; skipping layout
06-09 07:22:03.136 1334-1334/Abc.com E/RecyclerView: No adapter attached; skipping layout
06-09 07:22:03.193 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe9ba5920
06-09 07:22:03.371 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2770
06-09 07:22:03.602 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:03.602 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xde257ec0, error=EGL_SUCCESS
06-09 07:22:04.094 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.010 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.010 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.010 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.010 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.011 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.011 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.012 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.013 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.017 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.242 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:06.242 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xde0bd5c0, error=EGL_SUCCESS
06-09 07:22:06.401 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2380
06-09 07:22:06.409 1334-1353/Abc.com D/OpenGLRenderer: endAllStagingAnimators on 0xde146600 (RippleDrawable) with handle 0xe8d3b660
06-09 07:22:09.669 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2150
06-09 07:22:16.830 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:16.830 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe8d614a0, error=EGL_SUCCESS

这里是错误Logcat:

06-09 07:21:37.993 1334-1334/? E/libprocessgroup: failed to make and chown /acct/uid_10011: Read-only file system
06-09 07:21:42.633 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2150
06-09 07:21:53.100 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:21:55.172 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe9ba5920
06-09 07:21:56.128 1334-1334/Abc.com E/shape: HEART,
06-09 07:21:58.203 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:22:02.946 1334-1334/Abc.com E/RecyclerView: No adapter attached; skipping layout
06-09 07:22:03.136 1334-1334/Abc.com E/RecyclerView: No adapter attached; skipping layout
06-09 07:22:03.193 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe9ba5920
06-09 07:22:03.371 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2770
06-09 07:22:04.094 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:22:06.401 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2380
06-09 07:22:09.669 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2150

【问题讨论】:

  • StackTrace好吗?
  • 请发布您的 logcat 错误。没有它们,就更难追踪错误。
  • logcat 的详细或错误?
  • @ChiragRaval 检查我的更新问题
  • 请粘贴显示应用程序错误的堆栈跟踪。

标签: android pdf itext


【解决方案1】:

在您的日志中,我看到了这一行:

06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>

通知awt。 AWT 是 Java 的一部分,在 Android 上不可用。您正在使用 iText for Java(哪个版本???),但您应该使用 Android 端口,iTextG。它在功能上与 iText 相同,但去掉了所有在 Android 上不可用的东西。

iTextG 在 Maven Central 上可用:https://mvnrepository.com/artifact/com.itextpdf/itextg 所以你可以在 Gradle 或任何你喜欢的构建系统中使用它。

【讨论】:

  • 我正在使用的 itext 5.5.10 版本
  • 那是安卓的错误版本。您必须使用 iTextG。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
  • 2014-01-19
  • 2017-10-20
相关资源
最近更新 更多