Wywołuję wiele programów obsługi przez new Handler (). PostDelayed (new Runnable () ..... Jak mogę to zatrzymać, gdy kliknę wstecz?
public class MyActivity extends AppCompatActivity implements OnClickListener {
private Button btn;
private Handler handler;
private Runnable myRunnable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn = (Button) findViewById(R.id.trainingsstart);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Handler handler = new Handler();
Runnable myRunnable = new Runnable() {
public void run() {
// Things to be done
}
};
handler.postDelayed(myRunnable, 3000);
}
@Override
public void onBackPressed() {
super.onBackPressed();
handler.removeCallbacks(myRunnable);
}
}
Mam problem z tym, że moje programy obsługi nie mają nazwy takiej jak „myHandler”. Jak sobie z tym poradzić?
Dzięki za pomoc!
Teraz mam ten błąd:
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)