错误NullPointerException试图调用传递的方法

时间:2015-02-25 10:45:59

标签: java android reflection nullpointerexception

主要活动有:

public class Demo {

    public void main() {
        Class[] paramTypes = new Class[1];
        parameterTypes[0] = Integer.Type;// tried also int.class;
        Method method1 = Demo.class.getMethod("method1", parameterTypes);

        OtherClass.askValue(this, new Demo(), method1, 100);
    }

    public void method1(int value) {
        System.out.println(value);
    }

}

在其他java文件的其他类中:

public class OtherClass {

    private Context ctx;
    private Object obj;
    private Method mtd;
    private int ris;

    public void askValue (Context context, Object object, Method method, int result) {
        ctx = context;
        obj = object;
        mtd = method;
        res = result;

        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setTitle("title");
        builder.setMessage("This is the message");
        builder.setCancelable(false);
        builder.setPositiveButton(R.string.yes, DIonClickPositive);
        builder.setNegativeButton(R.string.no, DIonClickNegative);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

     private DialogInterface.OnClickListener() DIonClickPositive = DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
                // if this button is clicked, close

                Object[] parameters = new Object[1];
                parameters[0] = res;
                try {
                     mtd.invoke(obj, parameters);
                } catch (InvocationTargetException ite) {
                     Toast.makeText (ctx, "EXC ITE ["+ite.getCause()+"]", Toast.LENGTH_SHORT).show();
                }

                dialog.dismiss();
          }
     }

     private DialogInterface.OnClickListener() DIonClickNegative = DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
                // if this button is clicked, close
                dialog.dismiss();
          }
     }

}

当我确认对话框(按下正按钮)时,它应该调用方法“method1”,而是输入异常。该异常的getCause()是“NullPointerException”。 在android手册中写道,它是在调用的第一个Object为“null”时引起的。 我查了一下,但它不是空的。

if (obj == null) Toast.makeText(ctx, "OBJ NULL !!!", Toast.LENGTH_SHORT).show();

没有显示吐司,所以它不是空的。

我该如何解决?

Thanksss

PS:我使用了这个例子“click here”,Blaise Doughan的帖子。

Eclipse控制台日志(它是堆栈跟踪?):

[2015-02-25 20:49:47 - Traffic] Dx 
trouble writing output: already prepared
[2015-02-25 20:49:50 - Traffic] ------------------------------
[2015-02-25 20:49:50 - Traffic] Android Launch!
[2015-02-25 20:49:50 - Traffic] adb is running normally.
[2015-02-25 20:49:50 - Traffic] Performing com.example.traffic.Repetitions activity launch
[2015-02-25 20:50:40 - Traffic] Uploading Traffic.apk onto device 'emulator-5554'
[2015-02-25 20:50:41 - Traffic] Installing Traffic.apk...
[2015-02-25 20:50:54 - Traffic] Success!
[2015-02-25 20:50:54 - Traffic] Starting activity com.example.traffic.Repetitions on device emulator-5554
[2015-02-25 20:50:55 - Traffic] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.traffic/.Repetitions }

1 个答案:

答案 0 :(得分:0)

我想在调用

之后
Method method1 = Demo.class.getMethod("method1", parameterTypes);

method1将是null。这是因为没有方法

method1(Integer arg0)
设置

后调用的

parameterTypes[0] = Integer.Type

如果您以后致电

mtd.invoke(obj, parameters);

您将获得NullPointerException

在您的演示课程中,您可以定义方法

public void method1(Integer value) {
    System.out.println(value.intValue());
}

这应该有效。