title: App crashes on clicking on same item
body: <p>I have this adapter plays music on button click. Its all working when I click a song that is already playing to stop, the app crashes. Can someone help me with this one - I'm trying to figure out for two hours now.</p>
  
  <p>Here is my adapter</p>
  
  <pre><code> public class DownloadsDetailsAdapter extends BaseAdapter {
      private Activity activity;
      private ArrayList&lt;Songs&gt; 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&lt;Void, Void, Void&gt; task;
      Handler handler = new Handler();
  
      public DownloadsDetailsAdapter(Activity a, ArrayList&lt;Songs&gt; 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()&lt;=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() &lt; 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 &amp;&amp; mediaPlayer != null) {
                          task.cancel(true);
                          mediaPlayer.release();
                          if (oldPosition != -1) {
                              ((DownloadedDetails) activity)
                                      .changePicToPlay(oldPosition);
                          }
  
                      }
  
                      if (position != oldPosition) {
                          ((DownloadedDetails) activity)
                                  .changePicToPlay(position);
                          task = new AsyncTask&lt;Void, Void, Void&gt;() {
                              @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&lt;Void, Void, Void&gt;() {
                                  @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;
      }
  }
  </code></pre>
  
  <p>Here is log cat:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And here is the album details line 151</p>
  
  <pre><code>public void changePicToPlay(int position) {
          songs.get(position).togglePlaying();
          adapter.notifyDataSetChanged();
      }
  </code></pre>