使用groovy的MissingMethodException

时间:2017-01-10 17:09:23

标签: java android groovy

我试图与pdfbox和groovy一起工作。我编写了一个代码,它以字符串格式获取一些信息,并使用该信息创建一个pdf文档。我想将此代码集成到我正在创建的Android应用程序中。

我在intellij IDEA中运行了代码并且它工作得非常好,但是当我尝试在Android Studio中运行代码时,我在代码的某些部分得到了一个MissingPropertyException,这在Android Studio上完全没有效果。此部分中的代码使用PDRectangle类中名为getWidth()的方法。我确信在这个特定的类中有一个这样的方法。那么为什么我仍然继续得到这个MissingMethodExceptions?

错误输出:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.silverfix.groovytestapp, PID: 5723
                  groovy.lang.MissingMethodException: No signature of method: org.apache.pdfbox.pdmodel.common.PDRectangle.getWidth() is applicable for argument types: () values: []
                  Possible solutions: getAt(java.lang.String), with(groovy.lang.Closure), getClass()
                      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
                      at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
                      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
                      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
                      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
                      at dgdlib.components.FormMetaHolder$_setupSizes_closure1.doCall(FormMetaHolder.groovy:41)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
                      at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
                      at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
                      at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
                      at groovy.lang.Closure.call(Closure.java:414)
                      at groovy.lang.Closure.call(Closure.java:430)
                      at org.codehaus.groovy.runtime.DefaultGroovyMethods.collect(DefaultGroovyMethods.java:3170)
                      at org.codehaus.groovy.runtime.DefaultGroovyMethods.collect(DefaultGroovyMethods.java:3140)
                      at org.codehaus.groovy.runtime.dgm$66.invoke(Unknown Source)
                      at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
                      at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
                      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
                      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
                      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
                      at dgdlib.components.FormMetaHolder.setupSizes(FormMetaHolder.groovy:26)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
                      at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:71)
                      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
                      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
                      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
                      at dgdlib.DGDExecuter.initialize(DGDExecuter.groovy:48)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
                      at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
                      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
                      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
                      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
                      at dgdlib.DGDExecuter.execute(DGDExecuter.groovy:36)
                      at com.silverfix.groovytestapp.MainActivity$1.onClick(MainActivity.java:52)
                      at android.view.View.performClick(View.java:4780)
                      at android.view.View$PerformClick.run(View.java:19866)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)

以下是我运行groovy代码的活动代码:

package com.silverfix.groovytestapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import dgdlib.Constants;
import dgdlib.DGDExecuter;
import dgdlib.components.entities.DGObject;


public class MainActivity extends AppCompatActivity {

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.create_doc);

        // DGObject class represents a specific object which has its own attributes that needs to be inserted to the pdf
        final List<DGObject> goods = new ArrayList();

        DGObject object = new DGObject("3456", "2.3", "Packing Group", "Description", "Qunatity Type"
                ,"Packing Instructions", "Authorization");

        goods.add(object);

        final Bundle properties = new Bundle();

        // Insert some information to this bundle

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DGDExecuter().execute(MainActivity.this, properties, goods);
            }
        });
    }
}

这是FormMetaHolder类:

package app.components

import app.components.entities.FieldMeta
import org.apache.pdfbox.cos.COSName
import org.apache.pdfbox.pdmodel.PDResources
import org.apache.pdfbox.pdmodel.font.PDFont
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm
import org.apache.pdfbox.pdmodel.interactive.form.PDField
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField

/**
 * Created by David on 19/10/2016.
 */
class FormMetaHolder {
    def mapOfSizes = []

    FormMetaHolder() {

    }

    void setupSizes(PDAcroForm acroForm) {
        def fields = acroForm.getFields()

        fields.collect() { PDField field ->
            String name = field.getFullyQualifiedName()
            char lastChar = name[-1]

            if(lastChar == '1') {
                name = name.substring(0, name.size() - 1)
                lastChar = name[-1]
            }

            if(!Character.isDigit(lastChar)) {
                // Getting the real width of the text field's rectangle
                if(field instanceof PDTextField) {
                    PDAnnotationWidget widget = field.getWidgets().get(0)
                    float width = widget.getRectangle().getWidth()

                    // Getting the font appearance of the text field
                    String defaultAppearance = field.getDefaultAppearance()
                    String[] parts = defaultAppearance.split(" ")

                    COSName fontName = COSName.getPDFName(parts[0].substring(1));
                    float fontSize = Float.parseFloat(parts[1])

                    PDFont font
                    PDResources resources

                    resources = widget.getNormalAppearanceStream().getResources()
                    if(resources != null) {
                        font = resources.getFont(fontName)
                    }
                    if(font == null) {
                        font = acroForm.getDefaultResources().getFont(fontName)
                    }

                    // Adding the metadata regarding the field to the map object
                    FieldMeta meta = new FieldMeta(fieldName: name, fieldWidth: width, fontSize: fontSize, font : font)
                    mapOfSizes << meta

                }
            }
        }
    }

    FieldMeta getMetaData(String fieldName) {
        mapOfSizes.find {FieldMeta meta -> meta.fieldName == fieldName}
    }
}

具体地说,groovy代码中的部分在运行时失败:

PDAnnotationWidget widget = field.getWidgets().get(0)
PDRectangle rectangle = widget.getRectangle()

float width = rectangle.getWidth()

这里有什么问题?

以下是项目层次结构的图像:https://i.gyazo.com/70a3683f6f78d7b02d63defc4e7b2c2c.png

这是PDRectangle类的图像,特别是方法getWidth()的部分:https://i.gyazo.com/5086b7536c690005127b76fff41cae83.png

0 个答案:

没有答案