我有一个解析 JSON 对象并将数据放入 TwoDimentionalArrayList 类型的 2D ArrayList 的应用程序。
public TwoDimentionalArrayList<String> getRota(String carerID, String date){
try {
JSONObject jsonObject1 = new JSONObject(tok3);
int count = jsonObject1.names().length();
for(int i = 0; i < count; i++){
String s = new Integer(i).toString();
JSONObject jsonobject2 = (JSONObject) jsonObject1.get(s);
String startdate = jsonobject2.getString("StartDate");
String duration = jsonobject2.getString("Duration");
String callStatusName = jsonobject2.getString("CallStatusName");
String clientForeName = jsonobject2.getString("ClientForename");
String clientSurName = jsonobject2.getString("ClientSurname");
Log.e(TAG, "startdate = " + startdate);
Log.e(TAG, "duration = " + duration);
Log.e(TAG, "callStatusName = " + callStatusName);
Log.e(TAG, "clientForeName = " + clientForeName);
Log.e(TAG, "clientsurName = " + clientSurName);
ArrayList<String> arr = new ArrayList<String>();
arr.add(startdate);
arr.add(duration);
arr.add(callStatusName);
arr.add(clientForeName);
arr.add(clientSurName);
arrayList.add(i, arr);
}
return arrayList;
}// end of getRota
class TwoDimentionalArrayList<T> extends ArrayList<ArrayList<T>> {
private static final long serialVersionUID = 1L;
public void addToInnerArray(int index, T element) {
while (index >= this.size()) {
this.add(new ArrayList<T>());
}
this.get(index).add(element);
}
public void addToInnerArray(int index, int index2, T element) {
while (index >= this.size()) {
this.add(new ArrayList<T>());
}
ArrayList<T> inner = this.get(index);
while (index2 >= inner.size()) {
inner.add(null);
}
inner.set(index2, element);
}
}
.
我通过以下方式发送 arryList。
Bundle b = new Bundle();
b.putSerializable("rotaArray", rotaArray);
intent.putExtra("rotaArrayBundle", b);
startActivity(intent);
然后像这样在另一个 Activity 中检索它。
intent = this.getIntent();
Bundle bundle = intent.getBundleExtra("rotaArrayBundle");
@SuppressWarnings("unchecked")
TwoDimentionalArrayList<String> array = (TwoDimentionalArrayList<String>) bundle.get("rotaArray");
该应用程序似乎在将数组转换为 TwoDimentionalArrayList 时遇到问题,这是解析 JSON 的内部类。这是例外。
09-20 11:23:13.797: E/AndroidRuntime(9743): FATAL EXCEPTION: main
09-20 11:23:13.797: E/AndroidRuntime(9743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carefreegroup/com.carefreegroup.GetRota}: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.carefreegroup.LoginWebservice$TwoDimentionalArrayList
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.os.Looper.loop(Looper.java:156)
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.app.ActivityThread.main(ActivityThread.java:5045)
09-20 11:23:13.797: E/AndroidRuntime(9743): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 11:23:13.797: E/AndroidRuntime(9743): at java.lang.reflect.Method.invoke(Method.java:511)
09-20 11:23:13.797: E/AndroidRuntime(9743): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 11:23:13.797: E/AndroidRuntime(9743): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 11:23:13.797: E/AndroidRuntime(9743): at dalvik.system.NativeStart.main(Native Method)
09-20 11:23:13.797: E/AndroidRuntime(9743): Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.carefreegroup.LoginWebservice$TwoDimentionalArrayList
09-20 11:23:13.797: E/AndroidRuntime(9743): at com.carefreegroup.GetRota.onCreate(GetRota.java:27)
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.app.Activity.performCreate(Activity.java:4543)
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
09-20 11:23:13.797: E/AndroidRuntime(9743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
09-20 11:23:13.797: E/AndroidRuntime(9743): ... 11 more