Button为何一旦添加监听setOnClickListener之后就抛异常了呢?
我在自定义的一个View上添加了一个Button,添加之后很正常,但问题是一旦为这个Button添加监听后,程序就挂了,控制台抛异常,琢磨半天没明白,还请坛友看看
Activity类:
public class ActivityMain extends Activity {
private Button btnOK;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnOK = (Button)findViewById(R.id.btn);
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
this.setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.app.ViewMain
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100dip"
android:layout_y="100dip"
android:text="@string/hello"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/btn"/>
</FrameLayout>
public class ViewMain extends View {
public ViewMain(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
}
}
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): FATAL EXCEPTION: main
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.ActivityMain}: java.lang.NullPointerException
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at android.os.Looper.loop(Looper.java:123)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at java.lang.reflect.Method.invoke(Method.java:521)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at dalvik.system.NativeStart.main(Native Method)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): Caused by: java.lang.NullPointerException
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at com.app.ActivityMain.onCreate(ActivityMain.java:19)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-26 15:03:17.669: ERROR/AndroidRuntime(2197): ... 11 more