2015-06-15 27 views
0

我正在使用反射來編寫一個類,該類將從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 
+0

這通常表示一個版本不匹配,或者在這種情況下,潛在的混淆問題。 – chrylis

+0

Android是--annoyingly-沒有報告'NoSuchMethodException'的類只有參數(和方括號中的那些,進一步將它與桌面JDK區分開來)。你能發佈錯誤信息的完整堆棧跟蹤嗎?目前我猜想'Base'沒有一個帶有'JSONObject'的構造函數,或者它已被proguard重命名或刪除。 – dhke

+0

在那裏,希望有幫助。 – whiterabbit25

回答

0

如果我正確地理解了你,你有一個正在被添加到的對象列表,並且這樣做是通過在jArr中自反地調用一個JSONObject的構造函數來實現的。 NoSuchMethodException是一個運行時異常,當您嘗試在運行時調用一個方法時發生,該方法不存在或您的方法簽名錯誤。我最好的猜測是你試圖調用的構造函數不帶有JSONObject類型的參數。 newInstance方法應該使用與構造函數相同的參數列表。

0

我想你已經回答了你自己的問題。沒有JSONObject構造函數只需要一個JSONObject。錯誤就是這樣說的。

爲什麼不更改您的代碼:

for (int i = 0; i < jArr.length(); i++) { 
     objectList.add(jArr.getJSONObject(i)); 
} 
+0

因此它正在模擬,例如,objectList.add(new Person(jArr.getJSONObject(i))。 – whiterabbit25