我有一个很奇怪的问题,
我试图禁用切换按钮仅几秒钟,以使应用程序中的人们无法对它进行spamclick。

我的toggleButton具有此侦听器:

videoToggleListener = new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                switchVideoSource();
                switchBetweenSources.setEnabled(false);

                freezeToggle.schedule(new TimerTask(){

                    @Override
                    public void run() {

                        switchBetweenSources.setEnabled(false);
                        switchBetweenSources.setEnabled(true);

                    }

                }, 1000);


            }
        };


我的应用程序不会在setEnabled(false)上强制关闭,但会在setEnabled(true)上强制关闭。

任何想法 ?

我的原木猫:

02-10 13:45:30.500: E/AndroidRuntime(7285): FATAL EXCEPTION: Timer-0
02-10 13:45:30.500: E/AndroidRuntime(7285): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.view.View.invalidate(View.java:5255)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.widget.TextView.invalidateDrawable(TextView.java:3916)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.graphics.drawable.LayerDrawable.invalidateDrawable(LayerDrawable.java:298)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:300)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:227)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:99)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.graphics.drawable.Drawable.setState(Drawable.java:400)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.graphics.drawable.LayerDrawable.onStateChange(LayerDrawable.java:409)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.graphics.drawable.Drawable.setState(Drawable.java:400)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.view.View.drawableStateChanged(View.java:7374)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.widget.TextView.drawableStateChanged(TextView.java:2312)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.widget.CompoundButton.drawableStateChanged(CompoundButton.java:265)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.widget.ToggleButton.drawableStateChanged(ToggleButton.java:143)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.view.View.refreshDrawableState(View.java:7388)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at android.view.View.setEnabled(View.java:3147)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at fr.partisocialiste.psa.activity.VideosGridActivity$1$1.run(VideosGridActivity.java:101)
02-10 13:45:30.500: E/AndroidRuntime(7285):     at java.util.Timer$TimerImpl.run(Timer.java:284)
02-10 13:45:32.804: I/Process(7285): Sending signal. PID: 7285 SIG: 9


编辑:

我已经这样改变了我的听众:

videoToggleListener = new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                switchVideoSource();
                switchBetweenSources.setEnabled(false);

                freezeToggle.schedule(new TimerTask(){

                    @Override
                    public void run() {
                        runOnUiThread(new Runnable(){

                            @Override
                            public void run() {
                                switchBetweenSources.setEnabled(true);

                            }
                        });
                    }
                }, 2000);
            }
        };


谢谢大家,您真棒=)!

最佳答案

显然,问题是您尝试从非UI线程更改View。如果您说您没有在TimerTask的第一行得到此异常,而只是在第二行,则可能是因为您的ToggleButton已被禁用,并且不会更改View

要从UI线程以外的其他View更改Thread的状态,应使用runOnUiThread()

关于android - 关于重新启用ToggleButton的奇怪错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9228098/

10-10 19:33