title: Audio android does forces closes app
body: <p>I have created an audio player with a list view of songs, when the user clicks on an item of the list view the music player starts and a button to pause it, all this worked fine!</p>
  
  <p>My next stop i implemented a seek bar to update progress of the song and two text views one for current duration of song and one for total duration of song. I have written the code and everything seems to be fine but every time i run it keeps force closing</p>
  
  <p>Help please!</p>
  
  <p>the .Java class </p>
  
  <pre><code>public class Nasheeds extends ListActivity implements OnCompletionListener, SeekBar.OnSeekBarChangeListener {
  //ArrayList holds the data (as HashMaps) to load into the ListView
  ArrayList&lt;HashMap&lt;String,String&gt;&gt; list = new ArrayList&lt;HashMap&lt;String,String&gt;&gt;();
  //SimpleAdapter does the work to load the data in to the ListView
  private SimpleAdapter sa;
  private MediaPlayer mp;
  public boolean istrue = true;
   private ImageButton btnPrevious;
   private ImageButton btnNext;
  
   private SeekBar songProgressBar;
   private TextView songCurrentDurationLabel;
   private TextView songTotalDurationLabel;
  
   //Handler to update UI timer, progress bar
   private Handler mHandler = new Handler();;
   private Utilities utils;
   private int seekForwardTime = 5000; //500 milliseconds
   private int seekBackwardTime = 5000;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.nasheeds2);
  
  
  
      //HashMap links each line of data to the correct TextView
      HashMap&lt;String,String&gt; item;
      for(int i=0;i&lt;Nasheed.length;i++){
        item = new HashMap&lt;String,String&gt;();
        item.put( "line1", Nasheed[i][0]);
        item.put( "line2", Nasheed[i][1]);
  
        list.add( item );
      }
  
      sa = new SimpleAdapter(this, list,
              R.layout.nasheeds1,
              new String[] { "line1","line2" },
              new int[] {R.id.displayname, R.id.title});
  
      setListAdapter(sa);
  
  
  
      btnPrevious = (ImageButton) findViewById(R.id.prev);
      btnNext = (ImageButton)findViewById(R.id.next);
      songProgressBar = (SeekBar)findViewById(R.id.seekbar);
      songCurrentDurationLabel = (TextView)findViewById(R.id.songCurrentDurationLabel);
      songTotalDurationLabel = (TextView)findViewById(R.id.songTotalDurationLabel);
  
      utils = new Utilities();
  
      songProgressBar.setOnSeekBarChangeListener(this);
      mp.setOnCompletionListener(this);
  
  
  
  
  
  
    getListView().setOnItemClickListener(new OnItemClickListener() {
  
      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
              long arg3) {
          switch (arg2)
          {
          case 0:
              System.out.println("User selected option 1");
          //  mp.stop();
              if (mp != null &amp;&amp; mp.isPlaying()) {
                 mp.stop();
             }else
              mp = MediaPlayer.create(Nasheeds.this, R.raw.mok);
              mp.start();
  
              // MediaPlayer mp = MediaPlayer.create(Nasheeds.this, R.raw.mok);  
              //  mp.start(); 
  
              TextView tv=(TextView) findViewById(R.id.selectedfile);
              tv.setText("Playing "+ "Mountains of Mekkah, Zain Bikha");
  
              //set progress bar values
              songProgressBar.setProgress(0);
              songProgressBar.setMax(100);
  
              //updating progress bar
              updateProgressBar();
         /*     catch (IllegalArgumentException e){
                  e.printStackTrace();
              } catch (IllegalStateException e){
                  e.printStackTrace();
              } catch (IO Exception e){
                  e.printStackTrace();
              }*/
  
              break;
          case 1:
              System.out.println("User selected option 2");
              if (mp != null &amp;&amp; mp.isPlaying()) {
                 mp.stop();
             }else
              mp = MediaPlayer.create(Nasheeds.this, R.raw.hnim);
              mp.start();
  
          case 2:
  
              break;                  
          }
  
      }
  
    });
  
  
  
    View playbtn = findViewById(R.id.play);
  playbtn.setOnClickListener(new OnClickListener() {
  
      public void onClick(View v) {
          // TODO Auto-generated method stub
            if (istrue) {
                mp.pause();
                istrue = false;
            } else {
                mp.start();
                istrue = true;
            }
  
  
  
            }
  
  
  });
  
  
  btnPrevious.setOnClickListener(new View.OnClickListener() {
  
      public void onClick(View arg0) {
         /* if(currentSongIndex &gt; 0){
              playSong(currentSongIndex - 1);
              currentSongIndex = currentSongIndex - 1;
          }else{
              // play last song
              playSong(list.size() - 1);
              currentSongIndex = list.size() - 1;
          }*/
          System.out.println("Click Works");
  
      }
  });
  
  
  btnNext.setOnClickListener(new View.OnClickListener() {
  
      public void onClick(View v) {
          System.out.println("Click Works");
      }
  });
  }
  
  
  
  
  
  
  public void updateProgressBar(){
      mHandler.postDelayed(mUpdateTimeTask, 100);
    }
  
   private Runnable mUpdateTimeTask = new Runnable(){
        public void run(){
            long totalDuration = mp.getDuration();
            long currentDuration = mp.getCurrentPosition();
  
            //displaying total duration time
            songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
            //displaying time completed playing
            songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
  
            //updating progress bar
            int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
            songProgressBar.setProgress(progress);
  
            //running this thread after 100 milliseconds
            mHandler.postDelayed(this, 100);
        }
    };
  public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {
      // TODO Auto-generated method stub
  
  }
  public void onStartTrackingTouch(SeekBar seekBar) {
      mHandler.removeCallbacks(mUpdateTimeTask);
  }
  public void onStopTrackingTouch(SeekBar seekBar) {
      mHandler.removeCallbacks(mUpdateTimeTask);
      int totalDuration = mp.getDuration();
      int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);
  
      mp.seekTo(currentPosition);
      updateProgressBar();
  }
  
     private String[][] Nasheed =
          {{"Mountains of Mekkah","Zain Bikha"},
          {"His name is Mohammed","Kamal Uddin"},
          {"Hadith 3",".....add hadith"},};
  
  public void onCompletion(MediaPlayer mp) {
  
  
  }
  
  }
  </code></pre>
  
  <p>The Utils Class </p>
  
  <pre><code>package com.example.fuelfinder;
  
  public class Utilities {
   /**
   * Function to convert milliseconds time to
   * Timer Format
   * Hours:Minutes:Seconds
   * */
  public String milliSecondsToTimer(long milliseconds){
      String finalTimerString = "";
      String secondsString = "";
  
      // Convert total duration into time
         int hours = (int)( milliseconds / (1000*60*60));
         int minutes = (int)(milliseconds % (1000*60*60)) / (1000*60);
         int seconds = (int) ((milliseconds % (1000*60*60)) % (1000*60) / 1000);
         // Add hours if there
         if(hours &gt; 0){
             finalTimerString = hours + ":";
         }
  
         // Prepending 0 to seconds if it is one digit
         if(seconds &lt; 10){
             secondsString = "0" + seconds;
         }else{
             secondsString = "" + seconds;}
  
         finalTimerString = finalTimerString + minutes + ":" + secondsString;
  
      // return timer string
      return finalTimerString;
  }
  
  /**
   * Function to get Progress percentage
   * @param currentDuration
   * @param totalDuration
   * */
  public int getProgressPercentage(long currentDuration, long totalDuration){
      Double percentage = (double) 0;
  
      long currentSeconds = (int) (currentDuration / 1000);
      long totalSeconds = (int) (totalDuration / 1000);
  
      // calculating percentage
      percentage =(((double)currentSeconds)/totalSeconds)*100;
  
      // return percentage
      return percentage.intValue();
  }
  
  /**
   * Function to change progress to timer
   * @param progress -
   * @param totalDuration
   * returns current duration in milliseconds
   * */
  public int progressToTimer(int progress, int totalDuration) {
      int currentDuration = 0;
      totalDuration = (int) (totalDuration / 1000);
      currentDuration = (int) ((((double)progress) / 100) * totalDuration);
  
      // return current duration in milliseconds
      return currentDuration * 1000;
  }
  }
  </code></pre>
  
  <p>xml file 1
      
      </p>
  
  <pre><code>    &lt;TextView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/displayname"
              android:textSize="18dip"
              android:textStyle="bold"
              android:singleLine="true"
              android:ellipsize="end"/&gt;
  
      &lt;LinearLayout
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"&gt;
  
              &lt;TextView
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:id="@+id/title"
                      android:textSize="15dip"
                      android:singleLine="true"
                      android:ellipsize="end"
                      android:layout_weight="1.0"/&gt;
  
              &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/duration"
                      android:gravity="right"
                      android:textSize="15dip"
                      android:singleLine="true"
                      android:ellipsize="end"/&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>second xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"&gt;
  
      &lt;ListView 
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:id="@android:id/list"
          android:layout_weight="1.0"
          /&gt;
  
      &lt;LinearLayout
              android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@android:drawable/screen_background_light"
      android:padding="10dip"&gt;
  
      &lt;TextView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/selectedfile"
              android:text="Not file selected"
              android:textColor="@android:color/black"
              android:gravity="center_horizontal"
              android:singleLine="true"
              android:ellipsize="middle"/&gt;
  
      &lt;SeekBar
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/seekbar"
              android:max="100"
              android:paddingBottom="10dip"/&gt;
  
      &lt;LinearLayout
                      android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:gravity="center"
              android:background="@android:drawable/screen_background_light"&gt;
  
          &lt;TextView
              android:id="@+id/songCurrentDurationLabel"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="TextView" /&gt;
  
              &lt;ImageButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/prev"
                      android:src="@android:drawable/ic_media_previous"
                      android:onClick="doClick"/&gt;
  
              &lt;ImageButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/play"
                      android:src="@android:drawable/ic_media_play"
                      android:onClick="onClick"/&gt;
  
              &lt;ImageButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/next"
                      android:src="@android:drawable/ic_media_next"
                      android:onClick="doClick"/&gt;
  
              &lt;TextView
                  android:id="@+id/songTotalDurationLabel"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="TextView" /&gt;
  
      &lt;/LinearLayout&gt; 
      &lt;/LinearLayout&gt; 
  </code></pre>
  
  <p></p>
  
  <p>The LogCat:</p>
  
  <pre><code>12-07 14:43:02.262: E/AndroidRuntime(1009): FATAL EXCEPTION: main
  12-07 14:43:02.262: E/AndroidRuntime(1009): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.fuelfinder/com.example.fuelfinder.Nasheeds}:  java.lang.NullPointerException
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.os.Handler.dispatchMessage(Handler.java:99)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.os.Looper.loop(Looper.java:137)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.app.ActivityThread.main(ActivityThread.java:4745)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at java.lang.reflect.Method.invokeNative(Native Method)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at java.lang.reflect.Method.invoke(Method.java:511)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at dalvik.system.NativeStart.main(Native Method)
  12-07 14:43:02.262: E/AndroidRuntime(1009): Caused by: java.lang.NullPointerException
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at com.example.fuelfinder.Nasheeds.onCreate(Nasheeds.java:79)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.app.Activity.performCreate(Activity.java:5008)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
  12-07 14:43:02.262: E/AndroidRuntime(1009):     ... 11 more
  </code></pre>