gpt4 book ai didi

java - 使用文件名填充 ListView

转载 作者:行者123 更新时间:2023-12-02 06:56:02 26 4
gpt4 key购买 nike

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

最佳答案

您应该先初始化字符串数组:

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));

关于java - 使用文件名填充 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17335203/

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