0

我有这个适配器在按钮单击时播放音乐。当我单击已经播放的歌曲停止时,一切正常,应用程序崩溃。有人可以帮我解决这个问题 - 我现在想弄清楚两个小时。

这是我的适配器

 public class DownloadsDetailsAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<Songs> data;
    private static LayoutInflater inflater = null;
    private MediaPlayer mediaPlayer;
    private ImageView playSong;
    private String id;
    private DatabaseHelper helper;
    private ImageLoader loader;
    private int oldPosition = -1;
    private AsyncTask<Void, Void, Void> task;
    Handler handler = new Handler();

    public DownloadsDetailsAdapter(Activity a, ArrayList<Songs> songs, String id) {
        activity = a;
        data = songs;
        this.id = id;
        loader = new ImageLoader(a);
        mediaPlayer = new MediaPlayer();
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public void stopMusic() {
        try {
            mediaPlayer.stop();
            if (task.isCancelled() == false)
                task.cancel(true);
            playSong.setBackgroundResource(R.drawable.play_nr);
        } catch (Exception e) {
        }
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.downloads_details_item, parent,
                    false);
        helper = new DatabaseHelper(activity);
        helper.openDB();
        TextView songNumber = (TextView) vi
                .findViewById(R.id.down_songs_item_number);
        TextView albumTitle = (TextView) vi
                .findViewById(R.id.down_songs_item_album_title);
        TextView singer = (TextView) vi
                .findViewById(R.id.down_songs_item_album_singer);
        ImageView deleteSong = (ImageView) vi
                .findViewById(R.id.down_songs_item_del);
        ImageView thumb = (ImageView) vi
                .findViewById(R.id.down_songs_item_thumb);
        TextView feat = (TextView) vi
                .findViewById(R.id.down_songs_item_album_feat);

        loader.DisplayImage(data.get(position).getSONG_THUMB(), thumb);
        playSong = (ImageView) vi.findViewById(R.id.down_songs_item_download);
        if (!data.get(position).getIsPlaying())
            playSong.setBackgroundResource(R.drawable.play_downloaded_album);
        else
            playSong.setBackgroundResource(R.drawable.stop);
        albumTitle.setText(data.get(position).getSONG_TITLE());
        singer.setText(data.get(position).getSONG_ARTIST());
        // feat.setText(data.get(position).getFeat());
        // if(data.get(position).getFeat().length()<=1)
        feat.setVisibility(View.GONE);
        songNumber.setText(position + 1 + "");
        if (!data.get(position).getSONG_MP3()
                .equals("http://nelu.burduja.com/musica/songs_with/")) {

            deleteSong.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (!helper.isDBOpen()) {
                        helper.openDB();
                    }
                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            switch (which) {
                            case DialogInterface.BUTTON_POSITIVE:
                                if (!helper.isDBOpen()) {
                                    helper.openDB();
                                }
                                helper.removeDownloadedSong(id,
                                        data.get(position).getSONG_TITLE());
                                helper.removeSharedSong(data.get(position)
                                        .getSONG_TITLE(), id);
                                File file = new File(data.get(position)
                                        .getSONG_MP3());
                                file.delete();
                                if (helper.getDownloadedSongsForAlbum(id)
                                        .size() < 1) {
                                    helper.removeDownloadedAlbum(id);
                                }
                                Toast.makeText(activity, R.string.song_deleted,
                                        Toast.LENGTH_SHORT).show();
                                helper.closeDB();
                                break;

                            case DialogInterface.BUTTON_NEGATIVE:
                                dialog.dismiss();
                                break;
                            }
                        }
                    };
                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            activity);
                    builder.setMessage(R.string.are_you_sure)
                            .setPositiveButton(R.string.yes,
                                    dialogClickListener)
                            .setNegativeButton(R.string.no, dialogClickListener)
                            .show();
                    helper.closeDB();

                }

            });

            playSong.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    final ProgressDialog bufferingDialog = new ProgressDialog(
                            activity);
                    // ((DownloadedDetails) activity).changePicToPlay(position);
                    if (task != null && mediaPlayer != null) {
                        task.cancel(true);
                        mediaPlayer.release();
                        if (oldPosition != -1) {
                            ((DownloadedDetails) activity)
                                    .changePicToPlay(oldPosition);
                        }

                    }

                    if (position != oldPosition) {
                        ((DownloadedDetails) activity)
                                .changePicToPlay(position);
                        task = new AsyncTask<Void, Void, Void>() {
                            @Override
                            protected void onPreExecute() {
                                bufferingDialog.setMessage(activity
                                        .getString(R.string.buffering));
                                bufferingDialog.show();
                                super.onPreExecute();
                            }

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

                                try {
                                    mediaPlayer = new MediaPlayer();
                                    mediaPlayer.setDataSource(data
                                            .get(position).getSONG_MP3());
                                    mediaPlayer.prepare();
                                    mediaPlayer.start();
                                    // handler.post(new Runnable() {
                                    //
                                    // @Override
                                    // public void run() {
                                    // ((DownloadedDetails) activity)
                                    // .changePicToPlay(position);
                                    // }
                                    // });
                                } catch (IllegalStateException e) {
                                    e.printStackTrace();

                                } catch (IOException e) {

                                    bufferingDialog.dismiss();
                                    ((DownloadedDetails) activity)
                                    .changePicToPlay(position);
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
//                                          ((DownloadedDetails) activity)
//                                                  .changePicToPlay(position);
                                            Toast.makeText(activity,
                                                    "Song not existing",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    });

                                    e.printStackTrace();
                                }

                                return null;
                            }

                            @Override
                            protected void onPostExecute(Void result) {
                                if (bufferingDialog.isShowing())
                                    bufferingDialog.dismiss();
                                super.onPostExecute(result);
                            }
                        };
                        task.execute();
                    } else {
                        if (data.get(position).getIsPlaying()==false) {
                            Log.v("--", "test0");
                            task = new AsyncTask<Void, Void, Void>() {
                                @Override
                                protected void onPreExecute() {

                                    super.onPreExecute();
                                }

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

                                    try {
                                        mediaPlayer = new MediaPlayer();
                                        mediaPlayer.setDataSource(data.get(
                                                position).getSONG_MP3());
                                        mediaPlayer.prepare();
                                        mediaPlayer.start();
                                        ((DownloadedDetails) activity)
                                                        .changePicToPlay(position);
                                        handler.post(new Runnable() {

                                            @Override
                                            public void run() {

                                            }
                                        });
//                                      mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
//                                          
//                                          @Override
//                                          public void onCompletion(MediaPlayer mp) {
//                                              ((DownloadedDetails) activity)
//                                              .changePicToPlay(position);
//                                          }
//                                      });

                                    } catch (IllegalStateException e) {
                                        e.printStackTrace();

                                    } catch (IOException e) {
                                        ((DownloadedDetails) activity)
                                        .changePicToPlay(position);
                                        handler.post(new Runnable() {
                                            @Override
                                            public void run() {

                                                Toast.makeText(activity,
                                                        "Song not existing",
                                                        Toast.LENGTH_SHORT)
                                                        .show();
                                                stopMusic();
                                            }
                                        });
                                    }

                                    // mediaPlayer.start();
                                    return null;
                                }

                                @Override
                                protected void onPostExecute(Void result) {

                                    super.onPostExecute(result);
                                }
                            };
                            task.execute();
                        } else {
                            Log.v("--", "test1");
                            task.cancel(false);
                            ((DownloadedDetails) activity)
                            .changePicToPlay(position);
                            handler.post(new Runnable() {

                                @Override
                                public void run() {
                                    playSong.setBackgroundResource(R.drawable.play_downloaded_album);

                                    Log.v("--", "test2");
                                }
                            });
                            Log.v("--", "test3");
                        }
                    }
                    oldPosition = position;
                }
            });
        }
        helper.closeDB();
        return vi;
    }
}

这是日志猫:

09-06 00:55:23.593: E/AndroidRuntime(15814): FATAL EXCEPTION: AsyncTask #5
09-06 00:55:23.593: E/AndroidRuntime(15814): java.lang.RuntimeException: An error occured while executing doInBackground()
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at java.lang.Thread.run(Thread.java:856)
09-06 00:55:23.593: E/AndroidRuntime(15814): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4056)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.view.View.requestLayout(View.java:12833)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.view.View.requestLayout(View.java:12833)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.view.View.requestLayout(View.java:12833)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.view.View.requestLayout(View.java:12833)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:273)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.view.View.requestLayout(View.java:12833)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.widget.AbsListView.requestLayout(AbsListView.java:1697)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:801)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:5613)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:35)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at com.outsourcefarm.musicagratis.activities.DownloadedDetails.changePicToPlay(DownloadedDetails.java:151)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at com.outsoucefarm.musicagratis.functions.DownloadsDetailsAdapter$2$2.doInBackground(DownloadsDetailsAdapter.java:258)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at com.outsoucefarm.musicagratis.functions.DownloadsDetailsAdapter$2$2.doInBackground(DownloadsDetailsAdapter.java:1)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-06 00:55:23.593: E/AndroidRuntime(15814):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-06 00:55:23.593: E/AndroidRuntime(15814):    ... 5 more

这是专辑详情第 151 行

public void changePicToPlay(int position) {
        songs.get(position).togglePlaying();
        adapter.notifyDataSetChanged();
    }
4

2 回答 2

0

由于不允许将 UI 元素放在 doInBackground() 中,因此出现错误。我没有深入分析你这样做了多少次,但乍一看我已经在你的 doInBackground() 方法中找到了这一行:

bufferingDialog.dismiss();

你应该在你的 onPostExecute()

在 doInBackground() 中应该只进行处理。所有 UI 都应该发生在 ASyncTask 的 onPreExecute 和 onPostExecute 中

于 2013-09-05T23:11:30.010 回答
0

我相信你的错误来自这里doInBackground()

     handler.post(new Runnable() {
                                        @Override
                                        public void run() {
//                                          ((DownloadedDetails) activity)
//                                                  .changePicToPlay(position);
                                            Toast.makeText(activity,
                                                    "Song not existing",
                                                    Toast.LENGTH_SHORT).show();
                                        }

没有必要使用 a Handler, Thread, etc... with an AsyncTask,它有涵盖使用所有这些的方法。使用AsyncTask它应该被使用的方式。用于publishProgress()调用onProgressUpdate()此处或返回值onPostExecute()以显示您的Toast

于 2013-09-05T23:10:16.880 回答