3

我在 logcat 中遇到错误:

03-23 07:53:22.422: E/AndroidRuntime(2603): FATAL EXCEPTION: main
03-23 07:53:22.422: E/AndroidRuntime(2603): java.lang.NoClassDefFoundError: java.awt.Color
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfChunk.color(PdfChunk.java:501)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:2651)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfDocument.flushLines(PdfDocument.java:2388)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfDocument.newPage(PdfDocument.java:772)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.pdf.PdfDocument.close(PdfDocument.java:940)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.lowagie.text.Document.close(Unknown Source)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.example.pdfexample.MainActivity.onCreate(MainActivity.java:26)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.Activity.performCreate(Activity.java:5104)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.os.Looper.loop(Looper.java:137)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at java.lang.reflect.Method.invoke(Method.java:511)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-23 07:53:22.422: E/AndroidRuntime(2603):     at dalvik.system.NativeStart.main(Native Method)
help me , thanks in advance
4

2 回答 2

4

您正在使用专为纯 Java 设计的库。Java API 和 Android API 之间存在一些细微但仍然显着的差异,主要与 gfx 相关。如您所见,Java Color 类在 Android 上没有严格的等价物。这就是导致您的错误的原因。

您可以找到支持 Android 的 PDF 库,或者使用远程服务来转换文档并直接将其下载为 PDF。

您可能会对此线程感兴趣:Android 的 PDF 库 - PDFBox?

于 2013-03-23T08:02:11.340 回答
0

您需要将正在使用的 jar 文件添加到构建路径中。

java.lang.NoClassDefFoundError 通常在您没有将 jar 文件添加到构建路径时发生

如何将jar文件添加到构建路径中

如果您在将 jars 添加到构建路径时遇到问题,请按照此链接进行 pdf 转换,最好的选择是使用iTextPdf 之类的东西

使用这个类并调用所需的函数来创建pdf

public class CreatePDF {

    private static Font normalFont = new Font(Font.FontFamily.TIMES_ROMAN, 25,
            Font.NORMAL, BaseColor.BLACK);

    private static Font Head = new Font(Font.FontFamily.TIMES_ROMAN, 35,
            Font.BOLD, BaseColor.BLACK);

    //Path is the path where you want your pdf to get stored
    public void createPDFDoc(ArrayList<notesWrapper> notesList,String path) {
        // TODO Auto-generated method stub
        Document document = new Document();
        try {
            PdfWriter.getInstance(document, new FileOutputStream(path));
            document.open();

            for(int i=0;i<notesList.size();i++)
            {               


                addContentHead(document,"Image "+(i+1));                
                addContent(document,notesList.get(i).message);
                if(i<notesList.size())
                {
                    document.newPage();
                }
            }

            document.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

    private void addContent(Document document,String content) throws DocumentException {


        Paragraph preface = new Paragraph();              
        addEmptyLine(preface, 1);

        if(!content.equalsIgnoreCase("insert note"))
        preface.add(new Paragraph(content, normalFont));

        else
            addEmptyLine(new Paragraph(), 1);

        addEmptyLine(preface, 3);
        document.add(preface);



    }


    private void addContentHead(Document document,String content) throws DocumentException {


        Paragraph preface = new Paragraph();              
        addEmptyLine(preface, 1);

        preface.add(new Paragraph(content, Head));
        addEmptyLine(preface, 3);
        document.add(preface);



    }



    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }


}
于 2013-03-23T08:01:34.523 回答