0

我正在使用反射编写一个类,该类将从 JSONArray jArr 的元素创建对象(我作为参数 Base 传递的类型),并将它们添加到 ArrayList objectList 中。一切都很好,直到我到达这一步:

for(int i = 0; i < jArr.length(); i++) {
        objectList.add(Base.getConstructor(new Class[]{JSONObject.class}).newInstance(jArr.getJSONObject(i)));
    }

在运行时,我收到以下错误:

java.lang.NoSuchMethodException: <init> [class org.json.JSONObject]

从我从谷歌收集的信息来看,当您尝试使用不正确的参数调用构造函数时,似乎会发生此错误,但我不确定为什么这会适用于此。

编辑:这是完整的错误,根据要求:

06-15 18:35:55.245 1919-1919/com.example.ben.phptest W/System.err: java.lang.NoSuchMethodException: [class org.json.JSONObject]
06-15 18:35:55.245 1919-1919 /com.example.ben.phptest W/System.err:在 java.lang.Class.getConstructorOrMethod(Class.java:472)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/ System.err:在 java.lang.Class.getConstructor(Class.java:446)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:在 com.example.ben .phptest.PHPList.(PHPList.java:58)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:在 com.example.ben.phptest.MainActivity.onCreate( MainActivity.java:35)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:在 android.app.Activity.performCreate(Activity.java:5231)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-15 18:35:55.255 1919- 1919/com.example.ben.phptest W/System.err:在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W /System.err﹕在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕在 android.app。 ActivityThread.access$800(ActivityThread.java:135)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:在 android.app.ActivityThread$H.handleMessage(ActivityThread.java :1196)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err:在 android.os.Handler.dispatchMessage(Handler.java:102)
06-15 18:35:55.265 1919- 1919/com.example.ben.phptest W/System.err:在 android.os.Looper.loop(Looper.java:136)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W /System.err:在 android.app.ActivityThread.main(ActivityThread.java:5017)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err:在 java.lang。 reflect.Method.invokeNative(Native Method)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err: at java.lang.reflect.Method.invoke(Method.java:515 )
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-15 18:35 :55.265 1919-1919/com.example.ben.phptest W/System.err:在 dalvik.system.NativeStart.main(Native Method)

(第 58 行是上面循环的核心)

附带说明一下,我传入的类绝对接受 JSONObject 作为其唯一且唯一的构造函数参数:

Person(JSONObject obj) throws JSONException
4

2 回答 2

0

如果我理解正确,您有一个正在添加的对象列表,并且您通过在 jArr 中的 JSONObject 上反射性地调用构造函数来执行此操作。NoSuchMethodException 是一个运行时异常,当您尝试在运行时调用不存在或您的方法签名错误的方法时会发生此异常。我最好的猜测是您尝试调用的构造函数不采用 JSONObject 类型的参数。newInstance 方法应采用与构造函数相同的参数列表。

于 2015-06-15T21:58:27.947 回答
0

我想你已经回答了你自己的问题。没有JSONObject构造函数只需要一个JSONObject. 错误就是这样说的。

为什么不将您的代码更改为:

for (int i = 0; i < jArr.length(); i++) {
        objectList.add(jArr.getJSONObject(i));
}
于 2015-06-15T22:03:42.840 回答