2016-05-30 6 views
1

タイマーを設定してタイマーを鳴らすことができます。 また、私はタイマーを停止する一時停止ボタンがあります。 一時停止するタイマーがないため、初めてアプリにアクセスするときに発生します。 クリックすると一時停止ボタンが表示されます。エラーが発生しました。カウントダウン時間を実行するかどうかを確認しますか?

long result = date2.getTime() - date1.getTime(); 

         timer = new CountDownTimer(result, 1000) { 


          public void onTick(long millisUntilFinished) { 
           time.setText(("seconds remaining: " + millisUntilFinished/1000)); 
           //create HashMap<Integer,String> textMap at the constructer of the adapter 
           //now fill this info int'o it 
           textMap.put(new Integer(position), "seconds remaining: " + millisUntilFinished/1000); 
           //notify about the data change 
           notifyDataSetChanged(); 

          } 

          public void onFinish() { 

           time.setVisibility(View.INVISIBLE); 
           //create HashMap<Integer,String> textMap at the constructer of the adapter 
           //now fill this info into it 

           textMap.put(new Integer(position),null); 
           //notify about the data change 
           notifyDataSetChanged(); 
           Toast.makeText(getApplicationContext(), "finish", Toast.LENGTH_LONG).show(); 
           if (rowView != lastview || mediaPlayer == null) { 
            play(position); 
            if (lastview != null) 
             lastview = rowView; 
           } else { 
            play.setImageResource(R.drawable.n_btn_play_unselected); 
            mediaPlayer.release(); 
            lastview = null; 
           } 


          } 

         }.start(); 
         deleteDialog.dismiss(); 
        } 
       }); 

、ここで私の一時停止ボタンされています:

 stop.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       time.setVisibility(View.INVISIBLE); 
       //create HashMap<Integer,String> textMap at the constructer of the adapter 
       //now fill this info into it 
       int position=0; 
       textMap.put(new Integer(position),null); 
       //notify about the data change 
       notifyDataSetChanged(); 

        timer.cancel(); 


      }   }); 

がどのように自分の時間をckeckするが実行されているかどうか、ここ は私のタイマーのですか? これをクリックすると、nullpointexecptionが返されます。あなたは次のことを行う必要があり、キャンセルする場合は、

if(timer != null){ 
    //Here the timer will be running 
} 

を:しかし

timer.cancel(); 
timer = null; 

をので、タイマーがnullでない場合、それが実行中であることを意味し、それが実行されているかどうかを確認するには

答えて

0

。 nullの場合は、ユーザーがキャンセルしたことを意味します。

+0

ありがとうございます。 – nsr

関連する問題