创建ListView并使用ArrayAdapter填充

时间:2013-10-22 18:42:13

标签: java android xml listview android-arrayadapter

我一直在尝试使用显示时间的数组填充此ListView。 程序中没有错误,但每当我运行它时,程序就会停止工作。

这是我的XML文件,其中包含String数组:

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">Services</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>

    <string-array name="Summer_Depart_Gozo">
        <item>00:45</item>
        <item>02:00</item>
        <item>03:30</item>
        <item>05:00</item>
        <item>06:00</item>
        <item>06:30(Mon-Fri Exc. PH)</item>
        <item>06.45(Sat, Sun And PH)</item>
        <item>07:00(Mon-Fri Exc. PH)</item>
        <item>07:30</item>
        <item>08:15</item>
        <item>09:00</item>
        <item>09:45</item>
        <item>10:30</item>
        <item>11:15</item>
        <item>12:00</item>
        <item>12:45</item>
        <item>13:30</item>
        <item>14:15</item>
        <item>15:00</item>
        <item>15:45</item>
        <item>16:30</item>
        <item>17:15</item>
        <item>18:00</item>
        <item>18:45</item>
        <item>19:30</item>
        <item>20:15</item>
        <item>21:00</item>
        <item>21:45</item>
        <item>23:00</item>
</string-array>

这是我的包含ID等的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ServicesActivity" >

<RadioGroup
    android:id="@+id/radioGroupDepart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
</RadioGroup>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/radioGroupDepart"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignLeft="@+id/radioGroupDepart"
    android:layout_alignParentRight="true"
    android:text="Button" />

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/button2"
    android:orientation="vertical" >
</LinearLayout>

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_alignTop="@+id/linearLayout1"
    android:layout_centerHorizontal="true"
    android:textAlignment="center" >

</ListView>

这是我的java代码:

    package com.example.services;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{

    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_services);

        listView = (ListView) findViewById(R.id.listView);
        String[] timetable = getResources().getStringArray(R.array.Summer_Depart_Gozo);
        listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,timetable));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.services, menu);   
        return true;
    }

}

任何帮助都将受到赞赏,因为我刚刚开始尝试Android应用程序开发,我正在尝试学习基础知识。谢谢:D

更新:LogCat

10-23 12:08:26.770: D/AndroidRuntime(801): Shutting down VM
10-23 12:08:26.770: W/dalvikvm(801): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-23 12:08:26.790: E/AndroidRuntime(801): FATAL EXCEPTION: main
10-23 12:08:26.790: E/AndroidRuntime(801): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.services/com.example.services.ServicesActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.services.ServicesActivity" on path: DexPathList[[zip file "/data/app/com.example.services-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.services-2, /system/lib]]
10-23 12:08:26.790: E/AndroidRuntime(801):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-23 12:08:26.790: E/AndroidRuntime(801):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-23 12:08:26.790: E/AndroidRuntime(801):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-23 12:08:26.790: E/AndroidRuntime(801):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-23 12:08:26.790: E/AndroidRuntime(801):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 12:08:26.790: E/AndroidRuntime(801):  at android.os.Looper.loop(Looper.java:137)
10-23 12:08:26.790: E/AndroidRuntime(801):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-23 12:08:26.790: E/AndroidRuntime(801):  at java.lang.reflect.Method.invokeNative(Native Method)
10-23 12:08:26.790: E/AndroidRuntime(801):  at java.lang.reflect.Method.invoke(Method.java:525)
10-23 12:08:26.790: E/AndroidRuntime(801):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-23 12:08:26.790: E/AndroidRuntime(801):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-23 12:08:26.790: E/AndroidRuntime(801):  at dalvik.system.NativeStart.main(Native Method)
10-23 12:08:26.790: E/AndroidRuntime(801): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.services.ServicesActivity" on path: DexPathList[[zip file "/data/app/com.example.services-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.services-2, /system/lib]]
10-23 12:08:26.790: E/AndroidRuntime(801):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
10-23 12:08:26.790: E/AndroidRuntime(801):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-23 12:08:26.790: E/AndroidRuntime(801):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-23 12:08:26.790: E/AndroidRuntime(801):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-23 12:08:26.790: E/AndroidRuntime(801):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
10-23 12:08:26.790: E/AndroidRuntime(801):  ... 11 more
10-23 12:09:01.160: D/AndroidRuntime(907): Shutting down VM
10-23 12:09:01.160: W/dalvikvm(907): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-23 12:09:01.180: E/AndroidRuntime(907): FATAL EXCEPTION: main
10-23 12:09:01.180: E/AndroidRuntime(907): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.services/com.example.services.ServicesActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.services.ServicesActivity" on path: DexPathList[[zip file "/data/app/com.example.services-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.services-1, /system/lib]]
10-23 12:09:01.180: E/AndroidRuntime(907):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-23 12:09:01.180: E/AndroidRuntime(907):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-23 12:09:01.180: E/AndroidRuntime(907):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-23 12:09:01.180: E/AndroidRuntime(907):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-23 12:09:01.180: E/AndroidRuntime(907):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 12:09:01.180: E/AndroidRuntime(907):  at android.os.Looper.loop(Looper.java:137)
10-23 12:09:01.180: E/AndroidRuntime(907):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-23 12:09:01.180: E/AndroidRuntime(907):  at java.lang.reflect.Method.invokeNative(Native Method)
10-23 12:09:01.180: E/AndroidRuntime(907):  at java.lang.reflect.Method.invoke(Method.java:525)
10-23 12:09:01.180: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-23 12:09:01.180: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-23 12:09:01.180: E/AndroidRuntime(907):  at dalvik.system.NativeStart.main(Native Method)
10-23 12:09:01.180: E/AndroidRuntime(907): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.services.ServicesActivity" on path: DexPathList[[zip file "/data/app/com.example.services-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.services-1, /system/lib]]
10-23 12:09:01.180: E/AndroidRuntime(907):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
10-23 12:09:01.180: E/AndroidRuntime(907):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-23 12:09:01.180: E/AndroidRuntime(907):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-23 12:09:01.180: E/AndroidRuntime(907):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-23 12:09:01.180: E/AndroidRuntime(907):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
10-23 12:09:01.180: E/AndroidRuntime(907):  ... 11 more

0 个答案:

没有答案