gpt4 book ai didi

android - 无法启动 Activity Java RunTime 异常

转载 作者:行者123 更新时间:2023-11-29 21:11:36 25 4
gpt4 key购买 nike

我只是启动一个 Activity ,该 Activity 具有从 ArrayList 生成的 ListView ,但它给了我异常这是我启动 Activity 的方式

Intent myIntent = new Intent(MainActivity.this, ViewInfo.class);
MainActivity.this.startActivity(myIntent);

这是 ViewInfo Activity 代码

public class ViewInfo extends Activity {

ArrayList<String> Users_List=new ArrayList<String>();
String[] lv_arr = {};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view_info_activity);

ReadXML();

lv_arr = (String[]) Users_List.toArray();
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter<String>(ViewInfo.this,
android.R.layout.simple_list_item_1, lv_arr));
}
public void ReadXML()
{ -------}
}

LogCat

03-12 12:51:44.456: A/dalvikvm(9202): Exception!!! threadid=1: thread exiting with uncaught exception (group=0x4001d7d8)
03-12 12:51:44.476: E/AndroidRuntime(9202): FATAL EXCEPTION: main
03-12 12:51:44.476: E/AndroidRuntime(9202): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignment1.reginfo/com.assignment1.reginfo.ViewInfo}: java.lang.ClassCastException: [Ljava.lang.Object;
03-12 12:51:44.476: E/AndroidRuntime(9202): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-12 12:51:44.476: E/AndroidRuntime(9202): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-12 12:51:44.476: E/AndroidRuntime(9202): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-12 12:51:44.476: E/AndroidRuntime(9202): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-12 12:51:44.476: E/AndroidRuntime(9202): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 12:51:44.476: E/AndroidRuntime(9202): at android.os.Looper.loop(Looper.java:123)
03-12 12:51:44.476: E/AndroidRuntime(9202): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-12 12:51:44.476: E/AndroidRuntime(9202): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 12:51:44.476: E/AndroidRuntime(9202): at java.lang.reflect.Method.invoke(Method.java:521)
03-12 12:51:44.476: E/AndroidRuntime(9202): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-12 12:51:44.476: E/AndroidRuntime(9202): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-12 12:51:44.476: E/AndroidRuntime(9202): at dalvik.system.NativeStart.main(Native Method)
03-12 12:51:44.476: E/AndroidRuntime(9202): Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
03-12 12:51:44.476: E/AndroidRuntime(9202): at com.assignment1.reginfo.ViewInfo.onCreate(ViewInfo.java:40)
03-12 12:51:44.476: E/AndroidRuntime(9202): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-12 12:51:44.476: E/AndroidRuntime(9202): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-12 12:51:44.476: E/AndroidRuntime(9202): ... 11 more
03-12 12:52:57.266: A/dalvikvm(9321): Exception!!! threadid=1: thread exiting with uncaught exception (group=0x4001d7d8)
03-12 12:52:57.286: E/AndroidRuntime(9321): FATAL EXCEPTION: main
03-12 12:52:57.286: E/AndroidRuntime(9321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignment1.reginfo/com.assignment1.reginfo.ViewInfo}: java.lang.ClassCastException: [Ljava.lang.Object;
03-12 12:52:57.286: E/AndroidRuntime(9321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-12 12:52:57.286: E/AndroidRuntime(9321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-12 12:52:57.286: E/AndroidRuntime(9321): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-12 12:52:57.286: E/AndroidRuntime(9321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-12 12:52:57.286: E/AndroidRuntime(9321): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 12:52:57.286: E/AndroidRuntime(9321): at android.os.Looper.loop(Looper.java:123)
03-12 12:52:57.286: E/AndroidRuntime(9321): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-12 12:52:57.286: E/AndroidRuntime(9321): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 12:52:57.286: E/AndroidRuntime(9321): at java.lang.reflect.Method.invoke(Method.java:521)
03-12 12:52:57.286: E/AndroidRuntime(9321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-12 12:52:57.286: E/AndroidRuntime(9321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-12 12:52:57.286: E/AndroidRuntime(9321): at dalvik.system.NativeStart.main(Native Method)
03-12 12:52:57.286: E/AndroidRuntime(9321): Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
03-12 12:52:57.286: E/AndroidRuntime(9321): at com.assignment1.reginfo.ViewInfo.onCreate(ViewInfo.java:41)
03-12 12:52:57.286: E/AndroidRuntime(9321): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-12 12:52:57.286: E/AndroidRuntime(9321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-12 12:52:57.286: E/AndroidRuntime(9321): ... 11 more
03-12 12:57:57.326: W/PrintK(9321): gnment1.reginfo(9321) sends SIGKILL to gnment1.reginfo(9321)

最佳答案

您的 logcat 清楚地表明您需要更改自

String[] lv_arr;

lv_arr = (String[]) Users_List.toArray();

lv_arr = new String[Users_List.size()];
lv_arr = Users_List.toArray(lv_arr);

关于android - 无法启动 Activity Java RunTime 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22603896/

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