导致空指针异常

时间:2013-09-24 05:43:43

标签: android exception

public class HanumanBhajan extends Activity implements SeekBar.OnSeekBarChangeListener{
private Handler newHandler = new Handler();
private Utilities utils;
private SeekBar songProgressBar;

PhoneStateListener phoneStateListener = null;
private ImageButton btnPlay = null;
private ImageButton btnRepeat = null;
private ImageButton btnStop = null;
MediaPlayer mp = null;
private boolean isRepeat = false;
private TextView txtSong = null;
public int currentimageindex=0;
private TextView txtCurrenSongDuration = null;
private TextView txtTotalDuration = null;
private boolean mpState = true;
Timer timer = null;
TimerTask task = null;
ImageView slidingimage = null;
private String path = null;
long milliseconds = 0;

HanumanBhajanList bhajanNo = new HanumanBhajanList();
private int[] IMAGE_IDS = {
        R.drawable.hanuman1,
        R.drawable.hanuman2,
        R.drawable.hanuman3,
        R.drawable.hanuman4,
        R.drawable.hanuman5,
        R.drawable.hanuman6,
        R.drawable.hanuman7,
        R.drawable.hanuman8,
        R.drawable.hanuman9,
        R.drawable.hanuman10
    };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.aartilayout);

    final Handler mHandler = new Handler();
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            AnimateandSlideShow();
        }
    };
    int delay = 1000; // delay for 1 sec.

    int period = 8000; // repeat every 4 sec.

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

    public void run() {

         mHandler.post(mUpdateResults);

    }

    }, delay, period);


 // All player buttons
    btnPlay = (ImageButton) findViewById(R.id.btnPlay);
    btnStop = (ImageButton) findViewById(R.id.btnStop);
    btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);

    txtCurrenSongDuration = (TextView) findViewById(R.id.songCurrentDurationLabel);
    txtTotalDuration = (TextView) findViewById(R.id.songTotalDurationLabel);

    if(bhajanNo.detailNo == 0){
        txtSong = (TextView) findViewById(R.id.songTitle);
        txtSong.setText("Hey Dukh Bhanjan");
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan0.mp3";
        File f = new File(path); 
        Uri uri = Uri.fromFile(f);   
        mp = MediaPlayer.create(HanumanBhajan.this, uri);
    }else if(bhajanNo.detailNo == 1){
        txtSong = (TextView) findViewById(R.id.songTitle);
        txtSong.setText("Aaj Mangalvar Hai");
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan1.mp3";
        File f = new File(path); 
        Uri uri = Uri.fromFile(f);   
        mp = MediaPlayer.create(HanumanBhajan.this, uri);
    }else if(bhajanNo.detailNo == 2){
        txtSong = (TextView) findViewById(R.id.songTitle);
        txtSong.setText("Jai Jai Jai Hanuman");
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan2.mp3";
        File f = new File(path); 
        Uri uri = Uri.fromFile(f);
        mp = MediaPlayer.create(HanumanBhajan.this, uri);
    }else if(bhajanNo.detailNo == 3){
        txtSong = (TextView) findViewById(R.id.songTitle);
        txtSong.setText("Mangal Moorti Maruti");
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan3.mp3";
        File f = new File(path); 
        Uri uri = Uri.fromFile(f);
        mp = MediaPlayer.create(HanumanBhajan.this, uri);
    }
    songProgressBar = (SeekBar) findViewById(R.id.songProgressBar); 
    songProgressBar.setOnSeekBarChangeListener(this);
    utils = new Utilities();
    songProgressBar.setProgress(0);
    songProgressBar.setMax(100);
    updateProgressBar();
   /* final Handler mHandler1 = new Handler();
    final Runnable mUpdateResults1 = new Runnable() {
        public void run() {

                    updateTime(mp);


        }
    };
    int delay1 = 0; // delay for 0 sec.

    int period1 = 1000; // repeat every 1 sec.

    Timer timer1 = new Timer();

    timer1.scheduleAtFixedRate(new TimerTask() {

    public void run() {

         mHandler1.post(mUpdateResults1);

    }

    }, delay1, period1);*/

    btnPlay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mp.setOnCompletionListener(new OnCompletionListener() {


                @Override
                public void onCompletion(MediaPlayer arg0) {
                    // TODO Auto-generated method stub
                    if(isRepeat){
                        // repeat is on play same song again
                        //Toast.makeText(getApplicationContext(), "Repeat is ON", Toast.LENGTH_SHORT).show();
                        //txtCount = (TextView) findViewById(R.id.textView1);
                        //mp.reset();
                        //mp = MediaPlayer.create(ShivaAarti.this, R.raw.shivaarti);
                        //x=0;
                        mp.start();
                        btnPlay.setImageResource(R.drawable.btn_pause);
                        //count++;
                        //String str = "Count: " + count;
                        //txtCount.setText("Count: " + count);


                    }else{
                        if(mp!=null){
                            //x=0;
                            mp.seekTo(0);
                            btnPlay.setImageResource(R.drawable.btn_play);
                        }

                    }
                }
            });
            if(mp.isPlaying()){
                if(mp!=null){
                    mp.pause();
                    //x = mp.getCurrentPosition();
                    // Changing button image to play button
                    btnPlay.setImageResource(R.drawable.btn_play);
                }
            }else{
                // Resume song
                if(mp!=null){
                    //mp.seekTo(x);
                    mp.start();
                    // Changing button image to pause button
                    btnPlay.setImageResource(R.drawable.btn_pause);
                }
            }


    }});
    btnStop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mp.isPlaying()){
                if(mp!=null){
                    mp.pause();
                    mp.seekTo(0);
                    //x=0;
                    //mp.seekTo(x);
                    btnPlay.setImageResource(R.drawable.btn_play);
                    //mp.seekTo(0);
                    //milliseconds = 0;
                }
            }else{
                //x=0;
                //mp.seekTo(x);
                //mp.pause();
                mp.seekTo(0);
                //milliseconds = 0;
            }
        }});    

    btnRepeat.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(isRepeat){
                isRepeat = false;
                Toast.makeText(getApplicationContext(), "Repeat is OFF", Toast.LENGTH_SHORT).show();
                btnRepeat.setImageResource(R.drawable.btn_repeat);
                mp.setLooping(false);
            }else{
                // make repeat to true
                isRepeat = true;
                Toast.makeText(getApplicationContext(), "Repeat is ON", Toast.LENGTH_SHORT).show();
                mp.setLooping(true);
                btnRepeat.setImageResource(R.drawable.btn_repeat_focused);
            }
        }
    });

    phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (state == TelephonyManager.CALL_STATE_RINGING) {
                if(mp.isPlaying()){
                if(mp!=null){
                    mp.pause();
                    // Changing button image to play button
                    btnPlay.setImageResource(R.drawable.btn_play);
                }
            }
            } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                if(mp!=null){
                    /*mp.seekTo(x);
                    mp.start();
                    // Changing button image to pause button
                    btnPlay.setImageResource(R.drawable.btn_pause);*/
                }
            } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                if(mp!=null){
                    mp.pause();
                    // Changing button image to play button
                    btnPlay.setImageResource(R.drawable.btn_play);
                }
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };
    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if(mgr != null) {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}
public void updateProgressBar() {
    newHandler.postDelayed(mUpdateTimeTask, 100);
} 
private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        long currentDuration = 0;
        long totalDuration=0;
        try{
        if(mpState==true){
        totalDuration = mp.getDuration();
        }
        }catch(Exception e){}
        try{
            if(mp!=null&&mpState==true){
                currentDuration = mp.getCurrentPosition();
            }else{
            }
            }catch (Exception e){

            }
        // Displaying Total Duration time
        txtTotalDuration.setText(""+utils.milliSecondsToTimer(totalDuration));
        // Displaying time completed playing
        txtCurrenSongDuration.setText(""+utils.milliSecondsToTimer(currentDuration));

        // Updating progress bar
        int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
        //Log.d("Progress", ""+progress);
        songProgressBar.setProgress(progress);

        // Running this thread after 100 milliseconds
        newHandler.postDelayed(this, 100);
    }
 };
 @Override
 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

 }
 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
     // remove message Handler from updating progress bar
     newHandler.removeCallbacks(mUpdateTimeTask);
 }
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
     newHandler.removeCallbacks(mUpdateTimeTask);
     int totalDuration = mp.getDuration();
     int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);

     // forward or backward to certain seconds
     mp.seekTo(currentPosition);

     // update timer progress again
     updateProgressBar();
 }  
/*private void updateTime( MediaPlayer mp){

    try{
    if(mp!=null&&mpState==true){
        milliseconds = mp.getCurrentPosition();

    }else{
    }
    }catch (Exception e){

    }
    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 > 0){
           finalTimerString = hours + ":";
       }

       // Prepending 0 to seconds if it is one digit
       if(seconds < 10){ 
           secondsString = "0" + seconds;
       }else{
           secondsString = "" + seconds;}
       if(minutes<10){
       finalTimerString = finalTimerString + "0"+ minutes + ":" + secondsString;
       }else{
           finalTimerString = finalTimerString + minutes + ":" + secondsString;

       }

       String finalTimerString1 = "";
        String secondsString1 = "";
        long milliseconds1 = 0;
        if(mpState==true){
            milliseconds1 = mp.getDuration();}
        // Convert total duration into time
           int hours1 = (int)( milliseconds1 / (1000*60*60));
           int minutes1 = (int)(milliseconds1 % (1000*60*60)) / (1000*60);
           int seconds1 = (int) ((milliseconds1 % (1000*60*60)) % (1000*60) / 1000);
           // Add hours if there
           if(hours1 > 0){
               finalTimerString1 = hours1 + ":";
           }

           // Prepending 0 to seconds if it is one digit
           if(seconds1 < 10){ 
               secondsString1 = "0" + seconds1;
           }else{
               secondsString1 = "" + seconds1;}
           if(minutes1<10){
           finalTimerString1 = finalTimerString1 + "0" + minutes1 + ":" + secondsString1;
           }else{
               finalTimerString1 = finalTimerString1 + minutes1 + ":" + secondsString1;
           }

    txtCurrenSongDuration.setText(""+finalTimerString);

    txtTotalDuration.setText(finalTimerString1);
}*/


private void AnimateandSlideShow() {

/*  slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
  slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);*/
  slidingimage=null;
  if(slidingimage!=null){
    ((BitmapDrawable)slidingimage.getDrawable()).getBitmap().recycle();
    slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
}
else{
    slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
}
currentimageindex++;

Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);

  slidingimage.startAnimation(rotateimage);

}

@Override 
public void onBackPressed(){

AlertDialog alertbox = new AlertDialog.Builder(this)
.setMessage("Do you want to exit player?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

    // do something when the button is clicked
    public void onClick(DialogInterface arg0, int arg1) {
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
        }
        mpState = false;
        //mmp.stop();
        mp.release();
        finish();


    }
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {

    // do something when the button is clicked
    public void onClick(DialogInterface arg0, int arg1) {
                   }
})
  .show();


}
//Option Menu
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
 menu.add(1,1,0,"Set as Ringtone");
 menu.add(1,2,1,"Set as Alarm");
 menu.add(1,3,2,"Rate this App");
 menu.add(1,4,3,"Exit");
 return true;
}
//Items selection in Option Menu
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
 switch(item.getItemId())
 {
 case 1:
     if(item.getTitle().equals("Set as Ringtone")){
         if(path != null){
             if(bhajanNo.detailNo == 0){
             this.runOnUiThread(show_toast);

             ContentValues values = new ContentValues();
             values.put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan0.mp3");
             values.put(MediaStore.MediaColumns.TITLE, "hanumanbhajan0.mp3");
             values.put(MediaStore.MediaColumns.SIZE, 5866529);
             values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
             values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

             //Insert it into the database
             Uri uri = MediaStore.Audio.Media.getContentUriForPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan0.mp3");
             getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan0.mp3" + "\"", null);
             Uri newUri = getContentResolver().insert(uri, values);

             RingtoneManager.setActualDefaultRingtoneUri(
               HanumanBhajan.this,
               RingtoneManager.TYPE_RINGTONE,
               newUri);
             }
             if(bhajanNo.detailNo == 1){
                 this.runOnUiThread(show_toast);

                 ContentValues values = new ContentValues();
                 values.put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan1.mp3");
                 values.put(MediaStore.MediaColumns.TITLE, "hanumanbhajan1.mp3");
                 values.put(MediaStore.MediaColumns.SIZE, 8610706);
                 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                 values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

                 //Insert it into the database
                 Uri uri = MediaStore.Audio.Media.getContentUriForPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan1.mp3");
                 getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan1.mp3" + "\"", null);
                 Uri newUri = getContentResolver().insert(uri, values);

                 RingtoneManager.setActualDefaultRingtoneUri(
                   HanumanBhajan.this,
                   RingtoneManager.TYPE_RINGTONE,
                   newUri);
             }
             if(bhajanNo.detailNo == 2){
                 this.runOnUiThread(show_toast);

                 ContentValues values = new ContentValues();
                 values.put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan2.mp3");
                 values.put(MediaStore.MediaColumns.TITLE, "hanumanbhajan2.mp3");
                 values.put(MediaStore.MediaColumns.SIZE, 5160448);
                 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                 values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

                 //Insert it into the database
                 Uri uri = MediaStore.Audio.Media.getContentUriForPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan2.mp3");
                 getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan2.mp3" + "\"", null);
                 Uri newUri = getContentResolver().insert(uri, values);

                 RingtoneManager.setActualDefaultRingtoneUri(
                   HanumanBhajan.this,
                   RingtoneManager.TYPE_RINGTONE,
                   newUri);
             }
             if(bhajanNo.detailNo == 3){
                 this.runOnUiThread(show_toast);

                 ContentValues values = new ContentValues();
                 values.put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan3.mp3");
                 values.put(MediaStore.MediaColumns.TITLE, "hanumanbhajan3.mp3");
                 values.put(MediaStore.MediaColumns.SIZE, 4156219);
                 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                 values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

                 //Insert it into the database
                 Uri uri = MediaStore.Audio.Media.getContentUriForPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan3.mp3");
                 getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan3.mp3" + "\"", null);
                 Uri newUri = getContentResolver().insert(uri, values);

                 RingtoneManager.setActualDefaultRingtoneUri(
                   HanumanBhajan.this,
                   RingtoneManager.TYPE_RINGTONE,
                   newUri);
             }
         }
     }
        break;
 case 2:
     if(item.getTitle().equals("Set as Alarm")){
         if(path != null){
             if(bhajanNo.detailNo == 0){
             this.runOnUiThread(show_toast1);

             ContentValues values = new ContentValues();
             values.put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan0.mp3");
             values.put(MediaStore.MediaColumns.TITLE, "hanumanbhajan0.mp3");
             values.put(MediaStore.MediaColumns.SIZE, 5866529);
             values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
             values.put(MediaStore.Audio.Media.IS_ALARM, true);

             //Insert it into the database
             Uri uri = MediaStore.Audio.Media.getContentUriForPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan0.mp3");
             getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan0.mp3" + "\"", null);
             Uri newUri = getContentResolver().insert(uri, values);

             RingtoneManager.setActualDefaultRingtoneUri(
               HanumanBhajan.this,
               RingtoneManager.TYPE_ALARM,
               newUri);
             }
             if(bhajanNo.detailNo == 1){
                 this.runOnUiThread(show_toast1);

                 ContentValues values = new ContentValues();
                 values.put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan1.mp3");
                 values.put(MediaStore.MediaColumns.TITLE, "hanumanbhajan1.mp3");
                 values.put(MediaStore.MediaColumns.SIZE, 8610706);
                 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                 values.put(MediaStore.Audio.Media.IS_ALARM, true);

                 //Insert it into the database
                 Uri uri = MediaStore.Audio.Media.getContentUriForPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan1.mp3");
                 getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan1.mp3" + "\"", null);
                 Uri newUri = getContentResolver().insert(uri, values);

                 RingtoneManager.setActualDefaultRingtoneUri(
                   HanumanBhajan.this,
                   RingtoneManager.TYPE_ALARM,
                   newUri);
             }
             if(bhajanNo.detailNo == 2){
                 this.runOnUiThread(show_toast1);

                 ContentValues values = new ContentValues();
                 values.put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan2.mp3");
                 values.put(MediaStore.MediaColumns.TITLE, "hanumanbhajan2.mp3");
                 values.put(MediaStore.MediaColumns.SIZE, 5160448);
                 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                 values.put(MediaStore.Audio.Media.IS_ALARM, true);

                 //Insert it into the database
                 Uri uri = MediaStore.Audio.Media.getContentUriForPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan2.mp3");
                 getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan2.mp3" + "\"", null);
                 Uri newUri = getContentResolver().insert(uri, values);

                 RingtoneManager.setActualDefaultRingtoneUri(
                   HanumanBhajan.this,
                   RingtoneManager.TYPE_ALARM,
                   newUri);
             }
             if(bhajanNo.detailNo == 3){
                 this.runOnUiThread(show_toast1);

                 ContentValues values = new ContentValues();
                 values.put(MediaStore.MediaColumns.DATA, Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan3.mp3");
                 values.put(MediaStore.MediaColumns.TITLE, "hanumanbhajan3.mp3");
                 values.put(MediaStore.MediaColumns.SIZE, 4156219);
                 values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                 values.put(MediaStore.Audio.Media.IS_ALARM, true);

                 //Insert it into the database
                 Uri uri = MediaStore.Audio.Media.getContentUriForPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan3.mp3");
                 getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.hindubhakti.android/.Hindu Bhakti/hanumanbhajan3.mp3" + "\"", null);
                 Uri newUri = getContentResolver().insert(uri, values);

                 RingtoneManager.setActualDefaultRingtoneUri(
                   HanumanBhajan.this,
                   RingtoneManager.TYPE_ALARM,
                   newUri);
             }
         }
     }
        break;
 case 3:
     Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
     Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
     Uri uri2 = Uri.parse("https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName());
     Intent goToBrowser = new Intent(Intent.ACTION_VIEW, uri2);
     try{
     try {
            startActivity(goToMarket);
        } catch (ActivityNotFoundException e) {
            startActivity(goToBrowser);
        }
     }catch (ActivityNotFoundException e){
         Toast.makeText(this, "Android Market & Browser Not Found!", Toast.LENGTH_SHORT).show();

     }
     break;
 case 4:
     if(item.getTitle().equals("Exit")){
         AlertDialog alertbox = new AlertDialog.Builder(this)
            .setMessage("Do you want to exit player?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                    if(mgr != null) {
                        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
                    }
                    mpState = false;
                    //mp.stop();
                    mp.release();
                    finish();


                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                               }
            })
              .show();
     }
     break;
}
 return super.onOptionsItemSelected(item);

}
private Runnable show_toast = new Runnable()
{
public void run()
{
    Toast.makeText(HanumanBhajan.this, "Ringtone Set!", Toast.LENGTH_SHORT)
                .show();
}
};
private Runnable show_toast1 = new Runnable()
{
public void run()
{
    Toast.makeText(HanumanBhajan.this, "Alarm Tone Changed, Create New!", Toast.LENGTH_SHORT)
                .show();
}
};


}

堆栈追踪:

java.lang.NullPointerException
at com.hindubhakti.android.HanumanBhajan$1.run(HanumanBhajan.java:309)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)

309行在Runnable mUpdateTimeTask中,首先是Try catch block。

我现在真的坚持这个错误。请帮忙! 我没有得到问题,请解释,以便我学习,而不仅仅是纠正它。 问候, 凯沙夫

1 个答案:

答案 0 :(得分:0)

如果没有给出更多信息(即行号),我认为mp中可能mUpdateTimeTask为空。这可能是因为您声明了private boolean mpState = true因此会抛出NPE:

if(mpState==true){
    totalDuration = mp.getDuration();
}