Converting ArrayList to an array causes crash
ArrayList<String> myArr = new ArrayList<String>();
then add some stuff to it
myArr.add("Hello");
myArr.add("Testing");
myArr.add("Testing 1 2 3 ");
And then I try to convert it to a String array by doing this:
String[] s = (String[])myArr.toArray();
I am going to use this array to eventually populate a listview. . . but it crash when casting and throws this
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.IRE.toolbox/org.IRE.toolbox.Mods}: java.lang.ClassCastException: [Ljava.lang.Object;
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2627)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.view.View.performClick(View.java:2408)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.view.View$PerformClick.run(View.java:8817)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.os.Handler.handleCallback(Handler.java:587)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.os.Looper.loop(Looper.java:144)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.app.ActivityThread.main(ActivityThread.java:4937)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at java.lang.reflect.Method.invoke(Method.java:521)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at com.android开发者_如何学JAVA.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at dalvik.system.NativeStart.main(Native Method)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at org.IRE.toolbox.Mods.onCreate(Mods.java:60)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
02-16 22:31:14.835: ERROR/AndroidRuntime(21666): ... 18 more
When I comment out that line it runs fine. Any ideas how I can do this with out it crashing, is it even possible?
http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html#toArray%28%29 documents clearly that the return type of toArray() is Object[]. Try
myArray.toArray(new String[0]);
String[] s = myArr.toArray(new String[myArr.size()]);
You can also try like this:
ArrayList<String> myArr = new ArrayList<String>();
myArr.add("Hello");
myArr.add("Testing");
myArr.add("Testing 1 2 3 ");
Object[] s = myArr.toArray();
System.out.print(s[0]);
System.out.print(s[1]);
System.out.print(s[2]);
精彩评论