我正在使用反射來編寫一個類,該類將從JSONArray jArr的元素中創建對象(其類型爲傳遞參數Base),並將它們添加到ArrayList objectList。一切都很好,直到我走到這一步:JSONObject NoSuchMethodException-初始化
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:at java.lang.Class.getConstructorOrMethod(Class.java:472)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:at java.lang.Class.getConstructor(Class.java:446)
06-15 18:35 :55.255 1919-1919/com.example.ben.phptest W/System.err:at com.example.ben.phptest.PHPList。(PHPList.java:58)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:at com.example.ben.phptest.MainActivity.onCreate(MainActivity.java:35)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:at android.app.Activity.performCreate(Activity.java:5231)
06-15 18:35:55.255 1919-1919 /com.example.ben.phptest W/System.err:at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:at android .app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err:at android.app.ActivityThread.access $ 800(ActivityThread.java:135)
06-15 18: 35:55.255 1919-1919/com.example.ben.phptest W/System.err:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
06-15 18:35:55.265 1919-1919 /com.example.ben.phptest W/System.err:at android.os.Handler.dispatchMessage(Handler.java:102)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err:at android.os.Looper.loop(Looper.java:136)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err:at android .app.ActivityThread.main(ActivityThread.java:5017)
06-15 18: 35:55.265 1919-1919/com.example.ben.phptest W/System.err:at 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:at 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:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err:at dalvik.system.NativeStart。主(本地方法)
(58號線是上述循環的心臟)
作爲一個方面說明,我傳遞的最明確接受一個JSONObject作爲其唯一的構造函數參數類:
Person(JSONObject obj) throws JSONException
這通常表示一個版本不匹配,或者在這種情況下,潛在的混淆問題。 – chrylis
Android是--annoyingly-沒有報告'NoSuchMethodException'的類只有參數(和方括號中的那些,進一步將它與桌面JDK區分開來)。你能發佈錯誤信息的完整堆棧跟蹤嗎?目前我猜想'Base'沒有一個帶有'JSONObject'的構造函數,或者它已被proguard重命名或刪除。 – dhke
在那裏,希望有幫助。 – whiterabbit25