0

我正在尝试实现一个自定义切换按钮类,可以使用 onClickListener 在其上识别触摸事件

但是,如果我在重写的 onClick() 方法中输入任何代码,应用程序就会崩溃。可能是什么问题?

我的自定义类如下所示:

package com.example.buttoncontrollertest;

import android.content.Context;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;

public class ButtonController extends ToggleButton implements OnClickListener{

    public static final int UPDATE_LED_SETTING = 1;
    private boolean enabled;
    private Handler uiHandler;

    public ButtonController(Context context) {
        super(context);
        init();
    }

    public ButtonController(Context context,AttributeSet attrs){
        super(context,attrs);
        init();
    }

    public ButtonController(Context context,AttributeSet attrs, int i){
        super(context,attrs,i);
        init();
    }

    private void init(){
        enabled = false;
        this.setChecked(false);
        this.setEnabled(false);
        super.setOnClickListener(this);
        uiHandler = null;
    }

    public void setHandler(Handler handler){
        uiHandler = handler;
    }

    @Override
    public void setEnabled(boolean en){
        enabled=en;
        super.setEnabled(en);

    }

    @Override
    public void onClick(View v) {
        Toast.makeText((Context)getApplicationWindowToken(), "button pressed", Toast.LENGTH_SHORT).show();  
    }       
}

我不能使用直接的 ToggleButton,因为这个新类也应该做很多其他的事情

以下是错误日志:

04-15 11:10:14.904: D/libEGL(7836): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
04-15 11:10:14.912: D/libEGL(7836): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
04-15 11:10:14.920: D/libEGL(7836): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
04-15 11:10:15.021: D/OpenGLRenderer(7836): Enabling debug mode 0
04-15 11:10:22.896: D/~~~~(7836): ~~~~ button pressed
04-15 11:10:22.896: D/AndroidRuntime(7836): Shutting down VM
04-15 11:10:22.896: W/dalvikvm(7836): threadid=1: thread exiting with uncaught exception (group=0x4144d2a0)
04-15 11:10:22.896: E/AndroidRuntime(7836): FATAL EXCEPTION: main
04-15 11:10:22.896: E/AndroidRuntime(7836): java.lang.ClassCastException: android.view.ViewRootImpl$W cannot be cast to android.content.Context
04-15 11:10:22.896: E/AndroidRuntime(7836):     at com.example.buttoncontrollertest.ButtonController.onClick(ButtonController.java:70)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at android.view.View.performClick(View.java:4222)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at android.widget.CompoundButton.performClick(CompoundButton.java:100)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at android.view.View$PerformClick.run(View.java:17337)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at android.os.Handler.handleCallback(Handler.java:615)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at android.os.Looper.loop(Looper.java:137)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at android.app.ActivityThread.main(ActivityThread.java:4895)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at java.lang.reflect.Method.invoke(Method.java:511)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-15 11:10:22.896: E/AndroidRuntime(7836):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

您应该使用 getContext() 而不是 (Context)getApplicationWindowToken()。

于 2013-04-15T05:50:53.547 回答