关于重新启用ToggleButton的奇怪错误

时间:2012-02-10 12:52:24

标签: android

我有一个非常奇怪的问题, 我尝试禁用切换按钮一秒钟,以使我的应用程序中的人无法通过垃圾邮件点击它。

我的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);
            }
        };

谢谢大家,你真棒=)!

2 个答案:

答案 0 :(得分:1)

您正在尝试从后台线程更新UI组件,Android不允许这样做。

查看创建一个充当回调的处理程序,这样每当TimerTask触发时,它都会触发启用和禁用按钮的处理程序(在UI线程上)。

答案 1 :(得分:1)

显然,问题是您尝试从非UI线程更改View。如果您说TimerTask内的第一行没有出现此异常,只是在第二行,这可能是因为您的ToggleButton已被禁用,这不会改变{{ 1}}。

要更改View与其他View的状态,您应该使用Thread。{/ p>