0
我想實現上觸摸事件可以使用onClickListener實施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();
}
}
我不能使用直接切換按鈕,因爲這個新的類也應該做了很多其他的東西
以下是錯誤日誌:
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)
你目前收到了什麼問題? –
發佈您的崩潰日誌 –
不要做一個Toast,而是添加日誌記錄:'Log.d(「~~~~」,「\ n \ n ~~~~按鈕按下」);'''' 。 – 18446744073709551615