我需要一种方法来向我展示ListView中存储的所有.txt文件。
所以,我写了这个函数但应用程序崩溃了。为什么?
主要活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvPlaylist);
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) {
ArrayList<File> mySongs = findTxT(Environment.getExternalStorageDirectory());
}
}
public ArrayList<File> findTxT(File root){
ArrayList<File> al = new ArrayList<File>();
File[] files = root.listFiles();
for(File singleFile : files){
if(singleFile.isDirectory()){
}else{
if(singleFile.getName().endsWith(".txt")){
al.add(singleFile);
}
}
}
return al;
}
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lvPlaylist"
android:layout_alignParentTop="true"
android:choiceMode="singleChoice" />
</RelativeLayout>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shon.rodry.elegantxt" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
堆栈跟踪:
10-20 09:38:50.320 3405-3405/? I/art﹕ Late-enabling -Xcheck:jni
10-20 09:38:50.387 3405-3405/com.shon.rodry.elegantxt W/System﹕ ClassLoader referenced unknown path: /data/app/com.shon.rodry.elegantxt-1/lib/arm
10-20 09:38:50.620 3405-3405/com.shon.rodry.elegantxt D/AndroidRuntime﹕ Shutting down VM
10-20 09:38:50.625 3405-3405/com.shon.rodry.elegantxt E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.shon.rodry.elegantxt, PID: 3405
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shon.rodry.elegantxt/com.shon.rodry.elegantxt.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.shon.rodry.elegantxt.MainActivity.findTxT(MainActivity.java:34)
at com.shon.rodry.elegantxt.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
答案 0 :(得分:1)
该方法应如下所示:
public ArrayList<File> findTxT(File root){
ArrayList<File> al = new ArrayList<File>();
File[] files = root.listFiles();
if(files == null) //This check will fix the crash
return al;
for(File singleFile : files){
if(singleFile.isDirectory()){
}else{
if(singleFile.getName().endsWith(".txt")){
al.add(singleFile);
}
}
}
return al;
}
您仍然需要找出数组为空的原因。您可能要么传递错误的根文件,要么可能是权限问题(权限未添加到清单和/或api 23 +上不允许的权限)。