使用文件名填充Listview

时间:2013-06-27 05:32:06

标签: java android listview

我正在编写一个应用程序,我需要列出特定forlder中存在的所有pdf文件。

但是现在我只想列出原始文件夹中的所有文件并填充ListView。

这是我到目前为止所做的:

Fields[] fields;
ListView list;
String[] sample;
 fields=R.raw.class.getFields();int count=0;
         for(Field f : fields)
         {
                     sample[count]=f.getName();count++;
         }

            list=getListView();
            list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_checked, sample));

然而,当我启动应用程序时,应用程序会一直崩溃。 我不知道我哪里出错了。

任何人都可以帮助我吗?

日志:

06-19 08:52:20.352: E/AndroidRuntime(4955): FATAL EXCEPTION: main
06-19 08:52:20.352: E/AndroidRuntime(4955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.magazine/com.example.magazine.MainActivity}: java.lang.NullPointerException: storage == null
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.os.Looper.loop(Looper.java:137)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at dalvik.system.NativeStart.main(Native Method)
06-19 08:52:20.352: E/AndroidRuntime(4955): Caused by: java.lang.NullPointerException: storage == null
06-19 08:52:20.352: E/AndroidRuntime(4955):     at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at java.util.Arrays.asList(Arrays.java:154)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at com.example.magazine.MainActivity.onCreate(MainActivity.java:44)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.app.Activity.performCreate(Activity.java:5104)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-19 08:52:20.352: E/AndroidRuntime(4955):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-19 08:52:20.352: E/AndroidRuntime(4955):     ... 11 more

1 个答案:

答案 0 :(得分:2)

你应该在之前初始化字符串数组:

Field[] fields = R.raw.class.getFields();
ListView list;
String sample[] = new String[R.raw.class.getFields().length];
int count=0;
 for(Field f : fields)
 {
     sample[count]=f.getName();
     count++;
 }

list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, sample));