致命异常错误

时间:2014-06-18 15:32:08

标签: android

我正在制作一个音乐播放器而且我想使用AsynTask。我的镜头就是它,但它似乎没有正常工作。 (致命异常AsyncTask#1 执行doInBackground时发生错误())。到目前为止我得到了什么:

public class LoadingScreenActivity extends Activity {

private SharedPreferences sharedPrefs;
private String oldsongsListJson;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    CurrentData init = new CurrentData(); // Used to initialize static fields in CurrentData

    // Load old songsList
    oldsongsListJson = sharedPrefs.getString("songsList", "null");
    if (!oldsongsListJson.equals("null")){
        loadOldSettings();
    }
    else{
        setContentView(R.layout.loading_screen);
        new LoadLibrary().execute();
    }
}

private void loadOldSettings(){
    // Parse oldsongsListJson and do all of the library setup.
    Gson gson = new Gson();
    Type listType = new TypeToken<ArrayList<Song>>() {}.getType();
    LibraryFiller libFill = new LibraryFiller(getApplicationContext());
    LibraryInfo initializer = new LibraryInfo();
    LibraryInfo.songsList = gson.fromJson(oldsongsListJson, listType);
    libFill.buildLibraryFromSongsList();
    Log.d("Library", "Old library Loaded");

    // Check if there is a stored current song; if so, load it.
    String oldSongJson = sharedPrefs.getString("currentSong", "null");
    if(!oldSongJson.equals("null")){
        CurrentData.currentSong = gson.fromJson(oldSongJson, Song.class);
    }
    else{
        CurrentData.currentSong = null;
    }

    // Load old song index
    CurrentData.currentSongIndex = sharedPrefs.getInt("currentSongIndex", 0);

    // Load old playlist position
    CurrentData.currentPlaylistPosition = sharedPrefs.getInt("currentPlaylistPosition", 0);

    // Check if there is a stored playlist; if so, load it, else initialize a new one.
    String oldPlaylistJson = sharedPrefs.getString("currentPlaylist", "null");

    if (!oldPlaylistJson.equals("null")){
        CurrentData.currentPlaylist = gson.fromJson(oldPlaylistJson, Playlist.class);
        Log.d("Library", "Old playlist Loaded:" + oldPlaylistJson);
    }
    else{
        CurrentData.currentPlaylist = new Playlist();
    }

    // Load the list of stored playlists
    String oldPlaylistsJson = sharedPrefs.getString("playlists", "null");

    if (!oldPlaylistsJson.equals("null")){
        Type playlistType = new TypeToken<ArrayList<Playlist>>() {}.getType();
        LibraryInfo.playlists = gson.fromJson(oldPlaylistsJson, playlistType);
    }
    else{
        LibraryInfo.playlists = new ArrayList<Playlist>();
    }

    // Load old shuffle status
    String shuffleHistory = sharedPrefs.getString("shuffleHistory", "null");
    Type intArrayType = new TypeToken<ArrayList<Integer>>() {}.getType();
    gson = new Gson();
    if(!shuffleHistory.equals("null") ){
        CurrentData.shuffleHistory = gson.fromJson(shuffleHistory, intArrayType);
    }
    String shuffleQueue = sharedPrefs.getString("shuffleQueue", "null");
    if(!shuffleQueue.equals("null") ){
        Type arrayType = new TypeToken<int[]>() {}.getType();
        CurrentData.shuffleQueue = gson.fromJson(shuffleQueue, arrayType);
    }
    CurrentData.shuffleHistoryPosition = sharedPrefs.getInt("shuffleHistoryPosition", 0);

    Intent i = new Intent(LoadingScreenActivity.this, MusicPlayerActivity.class);
    startActivity(i);
    finish();

}

/**
 * Task which scans the SD card for music files and fills in the LibraryInfo class.  
 */
private class LoadLibrary extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {

        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... arg0) {

        Log.d("Library", "No Library Loaded");
        LibraryFiller libFillAll = new LibraryFiller(getApplicationContext());
        libFillAll.loadLibrary();
        Gson gson = new Gson();
        String songsListJson = gson.toJson(LibraryInfo.songsList);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putString("songsList", songsListJson);
        editor.commit();

        // Look up the directory to search for music from the preferences activity
        //Log.d("Library", "No Library Loaded");
        //String library_location = sharedPrefs.getString("library_location", "null");
        //LibraryFiller libFill = new LibraryFiller(library_location);
        //if(libFill.loadLibrary() == -1){
            //Toast.makeText(getApplicationContext(), "Invalid Library folder, could not load music.",
            //Toast.LENGTH_LONG).show();
        //      }

        // Check if there is a stored playlist; if so, load it, else initialize a new one.
        String oldPlaylistJson = sharedPrefs.getString("currentPlaylist", "null");

        if (!oldPlaylistJson.equals("null")){
            CurrentData.currentPlaylist = gson.fromJson(oldPlaylistJson, Playlist.class);
            Log.d("Library", "Playlist Loaded");
        }
        else{
            CurrentData.currentPlaylist = new Playlist();
        }





        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Close this activity and launch MusicPlayerActivity
        Intent i = new Intent(LoadingScreenActivity.this, MusicPlayerActivity.class);
        startActivity(i);
        finish();
    }

这里是logcat:

    06-18 11:12:57.923: D/Library(1071): No Library Loaded
    06-18 11:12:58.133: W/dalvikvm(1071): threadid=11: thread exiting with uncaught exception  (group=0xb2af0ba8)   
    06-18 11:12:58.193: E/AndroidRuntime(1071): FATAL EXCEPTION: AsyncTask #1
    06-18 11:12:58.193: E/AndroidRuntime(1071): Process: com.andrewkiluk.machmusicplayer, PID: 1071
    06-18 11:12:58.193: E/AndroidRuntime(1071): java.lang.RuntimeException: An error occured while    executing doInBackground()
    06-18 11:12:58.193: E/AndroidRuntime(1071):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
    06-18 11:12:58.193: E/AndroidRuntime(1071):     at     java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    06-18 11:12:58.193: E/AndroidRuntime(1071):     at  java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    06-18 11:12:58.193: E/AndroidRuntime(1071):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    06-18 11:12:58.193: E/AndroidRuntime(1071):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    06-18 11:12:58.193: E/AndroidRuntime(1071):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    06-18 11:12:58.193: E/AndroidRuntime(1071):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    06-18 11:12:58.193: E/AndroidRuntime(1071):     at java.lang.Thread.run(Thread.java:841)
    06-18 11:12:58.193: E/AndroidRuntime(1071): Caused by: java.lang.SecurityException: Permission  Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media   from pid=1071, uid=10066 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at android.os.Parcel.readException(Parcel.java:1465)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at android.content.ContentResolver.query(ContentResolver.java:461)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at android.content.ContentResolver.query(ContentResolver.java:404)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at com.andrewkiluk.machmusicplayer.LibraryFiller.loadSongsListContentResolver(LibraryFiller.java:181)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at com.andrewkiluk.machmusicplayer.LibraryFiller.loadLibrary(LibraryFiller.java:74)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at com.andrewkiluk.machmusicplayer.LoadingScreenActivity$LoadLibrary.doInBackground(LoadingScreenActivit   y.java:127)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at com.andrewkiluk.machmusicplayer.LoadingScreenActivity$LoadLibrary.doInBackground(LoadingScreenActivit   y.java:1)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at android.os.AsyncTask$2.call(AsyncTask.java:288)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   06-18 11:12:58.193: E/AndroidRuntime(1071):  ... 4 more

1 个答案:

答案 0 :(得分:1)

正如这句话所说:

Caused by: java.lang.SecurityException: Permission  Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media   from pid=1071, uid=10066 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

您需要Android Manifest中的权限android.permission.READ_EXTERNAL_STORAGE

写下这个:

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