仅在删除多个音乐文件时出现NullPointer异常

时间:2015-03-27 14:15:18

标签: android android-file

我正在添加删除音乐播放器应用中歌曲的功能。

我有以下两种用于删除歌曲的方法。

public static boolean deleteSong(Context c, String songId) {
    long id = Long.parseLong(songId);
    Uri songUri = ContentUris.withSppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
    String filePath = getPathFromUri(c, songUri); // my custom method for getting file path
    // Delete file
    boolean deleted = (new File(filePath)).delete();
    // Delete from MediaStore database
    if(deleted)
        c.getContentResolver().delete(songUri, null, null);
    return deleted;
}

public static int deleteSongs(Context c, ArrayList<SongItem> songs) {
    int failednum = 0;
    boolean deleted;
    for(SongItem song : songs) {
        deleted = deleteSong(c, song.getId());
    if(!deleted)
        failednum++;
    return failednum; // return number of files that were not deleted
}

deleteSong()方法绝对没有任何问题。当deleteSongs() ArrayList很大(3个或更多项)时,songs方法也有效但经常(几乎总是),我得到NullPointer异常。无论如何,java.io.File.fixSlashes是什么?

logcat:

E/AndroidRuntime(25300): FATAL EXCEPTION: AsyncTask #5
E/AndroidRuntime(25300): Process: com.example.player, PID: 25300
E/AndroidRuntime(25300): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(25300):        at android.os.AsyncTask$3.done(AsyncTask.java:300)
E/AndroidRuntime(25300):        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime(25300):        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime(25300):        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime(25300):        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime(25300):        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime(25300):        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime(25300):        at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime(25300): Caused by: java.lang.NullPointerException
E/AndroidRuntime(25300):        at java.io.File.fixSlashes(File.java:185)
E/AndroidRuntime(25300):        at java.io.File.<init>(File.java:134)
E/AndroidRuntime(25300):        at com.example.player.Utils.deleteSong(Utils.java:190)
E/AndroidRuntime(25300):        at com.example.player.Utils.deleteSongs(Utils.java:202)
E/AndroidRuntime(25300):        at com.example.player.AllSongsFragment$DeleteSongsTask.doInBackground(AllSongsFragment.java:362)
E/AndroidRuntime(25300):        at com.example.player.AllSongsFragment$DeleteSongsTask.doInBackground(AllSongsFragment.java)
E/AndroidRuntime(25300):        at android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime(25300):        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(25300):        ... 4 more

这是getPathFromUri()方法

public static String getPathFromUri(Context c, Uri contentURI) {
    String result = "";
    Cursor cursor = c.getContentResolver().query(contentURI, null, null, null, null);
    if(cursor != null && cursor.moveToFirst()) {
        int columnIndex = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
        result = cursor.getString(columnIndex);
        cursor.close();
        return result;
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试使用null文件路径创建文件。尝试检查此行之前filePath是否为空:

boolean deleted = (new File(filePath)).delete();

试试这个:

boolean deleted = false;
String filePath = getPathFromUri(c, songUri); // my custom method for getting file path

// Delete file (if filePath isn't null)
if(filePath != null)
    deleted = (new File(filePath)).delete();
else
    Log.v("DEBUG", "filePath of song with id #" + songId + " is null");
...