你的activity_main.xml中使用Button的地方改成com.xxx.xxx.MyButton(就是你的 包名.MyButton),
那么MyButton my=(MyButton)findViewById(R.id.buttonOK);这句话是可以正常显示。

解决方案 »

  1.   

    谢谢ncepu307你的回答,但是在Button中没有找到你要求改的地方,我将XML代码显示,请教在现有的代码中如何写,谢谢:)
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >    <Button
            android:id="@+id/buttonOK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="62dp"
            android:text="Button" /></RelativeLayout>
      

  2.   

    <com.xxx.xxx.MyButton
            android:id="@+id/buttonOK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="62dp"
            android:text="Button" /></RelativeLayout>
      

  3.   

    你在layout布局文件中button要用你自定义的button的包名+类名
      

  4.   

    已经很明显了,XML中引用Button是系统自己的Button,自定义的必须给出完整的路径,也就是包名.MyButton