将ArrayList转换为数组会导致崩溃

时间:2011-02-17 04:37:00

标签: java android arrays

ArrayList<String> myArr = new ArrayList<String>();

然后添加一些东西

myArr.add("Hello");
    myArr.add("Testing");
    myArr.add("Testing 1 2 3 ");

然后我尝试通过这样做将其转换为String数组:

String[] s = (String[])myArr.toArray();

我将使用此数组最终填充listview。 。 。但是在投射时会崩溃并抛出此

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.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

当我注释掉那条线时它运行正常。 任何想法如何在崩溃的情况下做到这一点,甚至可能吗?

3 个答案:

答案 0 :(得分:5)

http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html#toArray%28%29明确说明toArray()的返回类型是Object []。尝试

myArray.toArray(new String[0]);

答案 1 :(得分:2)

String[] s =  myArr.toArray(new String[myArr.size()]);

答案 2 :(得分:1)

你也可以尝试这样:

    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]);