按下按钮一段时间以触发活动

时间:2017-01-19 12:12:42

标签: java android nullpointerexception

如果某个用户按下浮动动作按钮5秒钟,我想发出紧急信号(要求进行此测试)
我在堆栈溢出时发现了一个代码但是一旦我没有点击按钮2秒就会崩溃!我在想timer.cancel();不能正常运作...任何人都得到了解决方案吗?

这是代码:

FloatingActionButton floatingactionknop = (FloatingActionButton) findViewById(R.id.knop3);
            final Timer timer = new Timer();
            floatingactionknop.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    switch (arg1.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            //start timer
                            timer.schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    Intent callintent = new Intent(getIntent().ACTION_DIAL);
                                    callintent.setData(Uri.parse("tel:zuster"));
                                    startActivity(callintent);
                                }
                            }, 2000);
                            return true;
                        case MotionEvent.ACTION_UP:
                            //stop timer
                            timer.cancel();
                            return true;
                    }
                    return false;
                }
            });

我收到此错误代码:

 E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.IllegalStateException: Timer was canceled
                        at java.util.Timer.scheduleImpl(Timer.java:558)
                        at java.util.Timer.schedule(Timer.java:456)
                        at com.asiancorp.dezorggroep.MainActivity$2.onTouch(MainActivity.java:79)
                        at android.view.View.dispatchTouchEvent(View.java:9290)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                        at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
                        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
                        at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
                        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                        at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
                        at android.view.View.dispatchPointerEvent(View.java:9514)
                        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
                        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
                        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
                        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
                        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
                        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                        at android.os.MessageQueue.nativePollOnce(Native Method)
                        at android.os.MessageQueue.next(MessageQueue.java:323)
                        at android.os.Looper.loop(Looper.java:135)
                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.asiancorp.dezorggroep, PID: 7050
                  java.lang.IllegalStateException: Timer was canceled
                      at java.util.Timer.scheduleImpl(Timer.java:558)
                      at java.util.Timer.schedule(Timer.java:456)
                      at com.asiancorp.dezorggroep.MainActivity$2.onTouch(MainActivity.java:79)
                      at android.view.View.dispatchTouchEvent(View.java:9290)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                      at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
                      at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
                      at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
                      at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                      at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                      at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
                      at android.view.View.dispatchPointerEvent(View.java:9514)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                      at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
                      at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
                      at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
                      at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
                      at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                      at android.os.MessageQueue.nativePollOnce(Native Method)
                      at android.os.MessageQueue.next(MessageQueue.java:323)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Application terminated.

1 个答案:

答案 0 :(得分:0)

从您的堆栈跟踪中我可以看到,Timer无法调用回调,因为它已被取消

java.lang.IllegalStateException: Timer was canceled

我建议您使用Runnable和Handler。特别是:

Handler h = new Handler();
Runnable r = new Runnable() {
                @Override
                public void run() {
                    Intent callintent = new Intent(getIntent().ACTION_DIAL);
                    callintent.setData(Uri.parse("tel:zuster"));
                    startActivity(callintent);
                }
            }



floatingactionknop.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                switch (arg1.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //start timer
                        h.postDelayed(r,2000);
                        return true;
                    case MotionEvent.ACTION_UP:
                        //stop timer
                        h.removeCallbacks(r);
                        return true;
                }
                return false;
            }
        });