0

我正在编写一个应用程序,我需要在其中列出特定文件夹中存在的所有 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
4

1 回答 1

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));
于 2013-06-27T05:38:28.187 回答