J'appelle plusieurs gestionnaires par new Handler (). PostDelayed (new Runnable () ..... Comment puis-je l'arrêter si je clique sur le bouton de retour?
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);
}
}
J'ai le problème que mes gestionnaires n'ont pas un nom comme "myHandler". Comment puis-je le gérer? Merci pour l'aide!
Maintenant, j'ai ces erreurs:
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)
Vous pouvez utiliser:
Handler handler = new Handler()
handler.postDelayed(new Runnable())
Ou vous pouvez utiliser:
handler.removeCallbacksAndMessages(null);
Docs
public final final removeCallbacksAndMessages (Jeton d'objet)
Ajoutée dans l'API niveau 1 Supprimez les publications en attente de rappels et les messages envoyés dont obj est un jeton. Si le jeton est nul, tous les rappels et les messages seront supprimés.
Ou vous pouvez aussi faire comme suit:
Handler handler= new Handler()
Runnable myRunnable = new Runnable() {
public void run() {
// do something
}
};
handler.postDelayed(myRunnable,zeit_dauer2);
Ensuite:
handler.removeCallbacks(myRunnable);
Docs
public final void removeCallbacks (Runnable r)
Ajoutée dans l'API niveau 1 Supprimez toutes les publications en attente de Runnable r se trouvant dans la file d'attente des messages.
public final void removeCallbacks (Runnable r, Jeton d'objet)
Edit:
Change ça:
@Override
public void onClick(View v) {
Crunch();
Handler handler= new Handler();
Runnable myRunnable = new Runnable() {
À:
@Override
public void onClick(View v) {
Crunch();
handler= new Handler();
myRunnable = new Runnable() { /* ... */}
Parce que tu as le dessous. Déclaré avant onCreate
mais vous l'avez à nouveau déclaré, puis initialisé dans onClick
pour aboutir à un NPE.
Handler handler; // declared before onCreate
Runnable myRunnable;
Vous pouvez définir un booléen et le remplacer par false lorsque vous souhaitez arrêter le gestionnaire. Comme ça..
boolean stop = false;
handler.postDelayed(new Runnable() {
@Override
public void run() {
//do your work here..
if (!stop) {
handler.postDelayed(this, delay);
}
}
}, delay);
cela peut être vieux, mais pour ceux qui cherchent une réponse, vous pouvez utiliser ceci ...
public void stopHandler() {
handler.removeMessages(0);
}
à votre santé
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;
}