SoundCloud致命异常

时间:2015-08-01 17:17:40

标签: java android soundcloud

我有一个mp3下载应用程序的源代码,几个月前我上次使用它时工作正常。现在,当我尝试它时,当我从搜索结果中点击一首歌来下载它时,它会给我一个错误。这是错误:

08-01 13:06:09.227: E/AndroidRuntime(2075): FATAL EXCEPTION: main
08-01 13:06:09.227: E/AndroidRuntime(2075): Process: com.soundload, PID: 2075
08-01 13:06:09.227: E/AndroidRuntime(2075): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Boolean de.voidplus.soundcloud.Track.isStreamable()' on a null object reference
08-01 13:06:09.227: E/AndroidRuntime(2075):     at com.baixavideos.Downloader.onPostExecute(Downloader.java:41)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at com.baixavideos.Downloader.onPostExecute(Downloader.java:1)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at android.os.AsyncTask.finish(AsyncTask.java:636)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at android.os.AsyncTask.access$500(AsyncTask.java:177)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at android.os.Looper.loop(Looper.java:135)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at android.app.ActivityThread.main(ActivityThread.java:5254)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at java.lang.reflect.Method.invoke(Native Method)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at java.lang.reflect.Method.invoke(Method.java:372)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
08-01 13:06:09.227: E/AndroidRuntime(2075):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-01 13:06:11.353: W/System.err(2174): java.io.IOException: open failed: EACCES (Permission denied)
08-01 13:06:11.353: W/System.err(2174):     at java.io.File.createNewFile(File.java:941)
08-01 13:06:11.354: W/System.err(2174):     at de.voidplus.soundcloud.SoundCloud.<init>(Unknown Source)
08-01 13:06:11.354: W/System.err(2174):     at com.baixavideos.ui.MainActivity.<clinit>(MainActivity.java:63)
08-01 13:06:11.354: W/System.err(2174):     at java.lang.reflect.Constructor.newInstance(Native Method)
08-01 13:06:11.354: W/System.err(2174):     at java.lang.Class.newInstance(Class.java:1606)
08-01 13:06:11.354: W/System.err(2174):     at android.app.Instrumentation.newActivity(Instrumentation.java:1066)

Downloader.java:

package com.baixavideos;

import com.baixavideos.ui.SongDetails;

import de.voidplus.soundcloud.SoundCloud;
import de.voidplus.soundcloud.Track;

import android.os.AsyncTask;
import android.util.Log;

public class Downloader extends AsyncTask<String, Void, Track>{

    private int id;
    private SoundCloud soundcloud;
    private SongDetails ui;

    public Downloader(){
        soundcloud = new SoundCloud("cd07b57592f6d914f437f8ab2856363d", "841953bd718a3ceb427b3e3be170a014"); 
    }

    public int getId() {
        return id;
    }

    public Downloader setId(int id) {
        this.id = id;

        return this;
    }

    @Override
    protected Track doInBackground(String... arg0) {        
        Track track = soundcloud.getTrack(getId());
        return track;
    }

    @Override
    protected void onPostExecute(Track track) {
        super.onPostExecute(track);

        if(track.isStreamable()){
            getUi().onSongLoaded(track);
        } else {
            getUi().onStreamingError();
        }
    }

    public SongDetails getUi() {
        return ui;
    }

    public Downloader setUi(SongDetails ui) {
        this.ui = ui;

        return this;
    }   

}

任何人都知道这里发生了什么?

1 个答案:

答案 0 :(得分:0)

这听起来像您的应用的临时目录的权限问题。您的应用只能将文件放入其自己的应用目录中,并且出于某种原因,权限会被拒绝。这就是为什么它最终失败的原因是isStreamable()为null。

您还尝试添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE></uses-permission>

同样onPostExecute()你应该检查一下track!= null,然后捕获错误并记录下来。