无法启动Activity Java RunTime异常

时间:2014-03-24 08:02:04

标签: android xml android-activity

我只是启动一个活动,其列表视图是从ArrayList生成的,但它给了我异常 以下是我发布活动的方式

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

这里是ViewInfo活动代码

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)

1 个答案:

答案 0 :(得分:2)

您的logcat明确表示您需要更改

String[] lv_arr;

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

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