2014-03-28 140 views
48

我呼籲通過新的處理程序()。postDelayed(新的Runnable()的多個處理程序..... 我怎樣才能阻止它,如果我點擊返回按鈕?停止處理程序()。postDelay

public class Tag1 extends Oberklasse implements OnClickListener { 

public Button btn; 
//private Handler myHandler = new Handler(); 
Handler handler; 
Runnable myRunnable; 




@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tt1); 
    //Bildschirm soll anbleiben!!! 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    //Lautstärke BUttons enable 
    setVolumeControlStream(AudioManager.STREAM_MUSIC); 





    getActionBar().setTitle("Tag 1"); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 


    //tt1.xml beschriften 
    TextView textView = (TextView)findViewById(R.id.uebung1); 
    textView.setText("Übung 1:"); 

    TextView textView2 = (TextView)findViewById(R.id.uebung2); 
    textView2.setText("Übung 2:"); 

    TextView textView3 = (TextView)findViewById(R.id.uebung3); 
    textView3.setText("Übung 3:"); 

    TextView textView4 = (TextView)findViewById(R.id.uebung4); 
    textView4.setText("Übung 4:"); 

    TextView textView5 = (TextView)findViewById(R.id.uebung5); 
    textView5.setText("Übung 5:"); 

    TextView textView6 = (TextView)findViewById(R.id.uebung6); 
    textView6.setText("Übung 6:"); 

    TextView textView7 = (TextView)findViewById(R.id.uebung7); 
    textView7.setText("Übung 7:"); 






    //Button finden und Initialisieren! 
    btn = (Button) findViewById(R.id.trainingsstart); 
    btn.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 





    Crunch(); 

    Handler handler= new Handler(); 
    Runnable myRunnable = new Runnable() { 
    public void run() { 
     UnterarmLiegestütz1(); 
    } 
    }; 
    handler.postDelayed(myRunnable,3000); 

} 

@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
    uebung.stop(); 
    handler.removeCallbacks(myRunnable); 

} 






} 

我有我的處理程序沒有一個名稱,比如「將myHandler」的問題,我該如何處理它 感謝您的幫助

現在,我得到這些錯誤:?!

03-28 17:47:03.662: E/AndroidRuntime(1961): FATAL EXCEPTION: main 
03-28 17:47:03.662: E/AndroidRuntime(1961): java.lang.NullPointerException 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at de.basti12354.tage.uebungen.Tag1.onBackPressed(Tag1.java:120) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.app.Activity.onKeyUp(Activity.java:2159) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.KeyEvent.dispatch(KeyEvent.java:2647) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.app.Activity.dispatchKeyEvent(Activity.java:2389) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.os.MessageQueue.nativePollOnce(Native Method) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.os.MessageQueue.next(MessageQueue.java:132) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.os.Looper.loop(Looper.java:124) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at java.lang.reflect.Method.invoke(Method.java:525) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
03-28 17:47:03.662: E/AndroidRuntime(1961):  at dalvik.system.NativeStart.main(Native Method) 

回答

105

您可以使用

Handler handler = new Handler() 
handler.postDelayed(new Runnable() 

您可以使用

handler.removeCallbacksAndMessages(null); 

文檔

public final void removeCallbacksAndMessages (Object token) 

Added in API level 1 
Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed. 

OR

Handler handler= new Handler() 
Runnable myRunnable = new Runnable() { 
public void run() { 
    // do something 
} 
}; 
handler.postDelayed(myRunnable,zeit_dauer2); 

然後

handler.removeCallbacks(myRunnable); 

文檔

public final void removeCallbacks (Runnable r) 

Added in API level 1 
Remove any pending posts of Runnable r that are in the message queue. 

public final void removeCallbacks (Runnable r, Object token) 

編輯:

更改此

@Override 
public void onClick(View v) { 
Crunch(); 
Handler handler= new Handler(); 
Runnable myRunnable = new Runnable() { 

TO

@Override 
public void onClick(View v) { 
Crunch(); 
handler= new Handler(); 
myRunnable = new Runnable() { 

因爲你有以下。在onCreate之前聲明,但是您重新聲明並將其初始化爲onClick,導致NPE。

Handler handler; // declared before onCreate 
Runnable myRunnable; 
+0

有加的handler.removeCallbacks(myRunnable);我的onBackPressed()是這樣嗎? – basti12354

+0

@ user3433232使用它你想停止運行 – Raghunandan

+0

你可以看看我的代碼,我嘗試使用你的幫助,但現在我得到了錯誤,你可以在我的帖子中看到。 – basti12354

1

這可能是舊的,但對於那些尋找答案您可以使用此...

public void Stop() { 
    handler.removeMessages(0); 
} 

歡呼

3

您可以定義一個布爾值,當你想將其更改爲假停止處理程序。這樣的..

boolean stop = false; 

handler.postDelayed(new Runnable() { 
    @Override 
    public void run() { 

     //do your work here.. 

     if (!stop) { 
      handler.postDelayed(this, delay); 
     } 
    } 
}, delay); 
+0

它爲我工作! – Fortran

0
Boolean condition=false; //Instance variable declaration. 

//-----------------Inside oncreate--------------------------------------------------- 
    start =(Button)findViewById(R.id.id_start); 
     start.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       starthandler(); 

       if(condition=true) 
       { 
        condition=false; 
       } 


      } 
     }); 

     stop=(Button) findViewById(R.id.id_stoplocatingsmartplug); 

     stop.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       stophandler(); 

      } 
     }); 


    } 

//-----------------Inside oncreate--------------------------------------------------- 

public void starthandler() 
    { 

     handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 


       if(!condition) 
       { 
        //Do something after 100ms 


       } 

      } 
     }, 5000); 

    } 


    public void stophandler() 
    { 
     condition=true; 
    } 
+0

請解釋爲什麼這段代碼回答這個問題。 –

+0

假設你在XML開始和停止中有2個按鈕。 這段代碼演示瞭如何通過單擊開始按鈕啓動處理程序,並且可以通過停止按鈕停止。 –