gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:24:31 27 4
gpt4 key购买 nike

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

然后添加一些东西

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

然后我尝试通过这样做将它转换为字符串数组:

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

当我注释掉该行时,它运行良好。我有什么想法可以在不崩溃的情况下做到这一点,这有可能吗?

最佳答案

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

myArray.toArray(new String[0]);

关于java - 将 ArrayList 转换为数组会导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5025100/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com