本文介绍了停止处理程序()。postDelay的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我调用多个处理程序通过新的处理程序()。postDelayed(新的Runnable().....我怎么能阻止它,如果我点击返回按钮?
公共类Tag1中扩展Oberklasse实现OnClickListener {
公众按钮BTN;
//专用处理器将myHandler =新的处理程序();
处理程序处理程序;
可运行myRunnable;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.tt1);
// Bildschirm索尔anbleiben!
。getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Lautstärke按钮启用
setVolumeControlStream(AudioManager.STREAM_MUSIC);
getActionBar()的setTitle(变量1)。
getActionBar()setDisplayHomeAsUpEnabled(真)。
//tt1.xml beschriften
TextView中的TextView =(TextView中)findViewById(R.id.uebung1);
textView.setText(Übung1:);
TextView的textView2 =(TextView中)findViewById(R.id.uebung2);
textView2.setText(Übung2:);
TextView的textView3 =(TextView中)findViewById(R.id.uebung3);
textView3.setText(Übung3:);
TextView的textView4 =(TextView中)findViewById(R.id.uebung4);
textView4.setText(Übung4:);
TextView的textView5 =(TextView中)findViewById(R.id.uebung5);
textView5.setText(Übung5:);
TextView的textView6 =(TextView中)findViewById(R.id.uebung6);
textView6.setText(Übung6:);
TextView的textView7 =(TextView中)findViewById(R.id.uebung7);
textView7.setText(Übung7:);
//按钮芬登UND Initialisieren!
BTN =(按钮)findViewById(R.id.trainingsstart);
btn.setOnClickListener(本);
}
@覆盖
公共无效的onClick(视图v){
紧缩();
处理程序处理程序=新的处理程序();
可运行myRunnable =新的Runnable(){
公共无效的run(){
UnterarmLiegestütz1();
}
};
handler.postDelayed(myRunnable,3000);
}
@覆盖
公共无效onBack pressed(){
super.onBack pressed();
uebung.stop();
handler.removeCallbacks(myRunnable);
}
}
我有我的处理程序还没有一个名称,如将myHandler的问题。我该如何处理呢?感谢您的帮助!
现在我得到这些错误:
03-28 17:47:03.662:E / AndroidRuntime(1961年):致命异常:主要
03-28 17:47:03.662:E / AndroidRuntime(1961年):显示java.lang.NullPointerException
03-28 17:47:03.662:E / AndroidRuntime(1961年):在de.basti12354.tage.uebungen.Tag1.onBack pressed(Tag1.java:120)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.app.Activity.onKeyUp(Activity.java:2159)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.KeyEvent.dispatch(KeyEvent.java:2647)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.app.Activity.dispatchKeyEvent(Activity.java:2389)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662:E / AndroidRuntime(1961):在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3483)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3406)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3540)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3406)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3516)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.inputmethod.InputMethodManager $ PendingEvent.run(InputMethodManager.java:1982)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
03-28 17:47:03.662:E / AndroidRuntime(1961):在android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.os.MessageQueue.nativePollOnce(本机方法)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.os.MessageQueue.next(MessageQueue.java:132)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.os.Looper.loop(Looper.java:124)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在android.app.ActivityThread.main(ActivityThread.java:5103)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在java.lang.reflect.Method.invokeNative(本机方法)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在java.lang.reflect.Method.invoke(Method.java:525)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 17:47:03.662:E / AndroidRuntime(1961年):在dalvik.system.NativeStart.main(本机方法)
解决方案
您可以使用
处理程序处理程序=新的处理程序()
handler.postDelayed(新的Runnable()
您可以使用
handler.removeCallbacksAndMessages(空);
文档
公众最终无效removeCallbacksAndMessages(对象令牌)
新增的API级别1
删除回调的任何未决的帖子和发送的消息其obj为标记。如果标记为空,所有的回调和消息将被删除。
或
处理程序处理程序=新的处理程序()
可运行myRunnable =新的Runnable(){
公共无效的run(){
// 做一点事
}
};
handler.postDelayed(myRunnable,zeit_dauer2);
然后
handler.removeCallbacks(myRunnable);
文档
公众最终无效removeCallbacks(Runnable的R)
新增的API级别1
删除了Runnable为r的任何未决的职位是在消息队列中。
公众最终无效removeCallbacks(Runnable的R,对象令牌)
编辑:
更改此
@覆盖
公共无效的onClick(视图v){
紧缩();
处理程序处理程序=新的处理程序();
可运行myRunnable =新的Runnable(){
到
@覆盖
公共无效的onClick(视图v){
紧缩();
处理程序=新的处理程序();
myRunnable =新的Runnable(){
因为你有下文。前的onCreate
宣告但你重新声明,然后在的onClick
导致NPE。
处理程序处理; //前的onCreate声明
可运行myRunnable;
I call multiple Handlers by new Handler().postDelayed(new Runnable().....How can I stop it, if I click on return button?
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);
}
}
I have the problem that my Handlers haven't a name like "myHandler" . How can I handle it?THanks for help!
Now I got these error:
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)
解决方案
You can use
Handler handler = new Handler()
handler.postDelayed(new Runnable()
You can use
handler.removeCallbacksAndMessages(null);
Docs
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);
Then
handler.removeCallbacks(myRunnable);
Docs
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)
Edit:
Change this
@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() {
Coz you have the below. Declared before onCreate
but you re-declared and then initialized it in onClick
leading to NPE.
Handler handler; // declared before onCreate
Runnable myRunnable;
这篇关于停止处理程序()。postDelay的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!