2016-10-20 4 views
0

ListViewにStorageのすべての.txtファイルを表示する方法が必要です。Storageのme .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) 
+2

スタックトレースを提供できますか? – ehehhh

+3

ここにクラッシュログを投稿してください... –

+0

@ehehhh done :) – xRobot

答えて

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

あなたはまだ配列がnullである理由を見つける必要があります。あなたは間違ったルートファイルを渡しているかもしれませんし、パーミッションの問題かもしれません(マニフェストに加えられていないパーミッションやapi 23+で許可されていないパーミッション)。

+0

ありがとう今はもうクラッシュしません。私はAPI 10を使用して開発していますが、私はAndroid 6(API 23)を使っています。だからおそらく許可の問題ですが、上記のようにそれを明示するために追加しました。 – xRobot

+0

アプリのアプリ設定(電話)とアクセス権を試してから、そこに手動でストレージを有効にしてください。コードを実行してみてください。これが有効な場合は、アプリで許可を有効にする必要があります。 – Kelevandos

関連する問題