加载程序到android虚拟机报错: android.widget.RelativeLayout cannot be cast to android.widget.Button

 05-23 02:53:48.416: E/Trace(875): error opening trace file: No such file or directory (2)
 05-23 02:53:52.545: E/AndroidRuntime(875): FATAL EXCEPTION: main
 05-23 02:53:52.545: E/AndroidRuntime(875): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intelsocket/com.example.intelsocket.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.os.Handler.dispatchMessage(Handler.java:99)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.os.Looper.loop(Looper.java:137)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.app.ActivityThread.main(ActivityThread.java:5041)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at java.lang.reflect.Method.invokeNative(Native Method)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at java.lang.reflect.Method.invoke(Method.java:511)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at dalvik.system.NativeStart.main(Native Method)
 05-23 02:53:52.545: E/AndroidRuntime(875): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button
 05-23 02:53:52.545: E/AndroidRuntime(875):     at com.example.intelsocket.MainActivity.set_btn_status(MainActivity.java:275)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at com.example.intelsocket.MainActivity.onCreate(MainActivity.java:62)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.app.Activity.performCreate(Activity.java:5104)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 05-23 02:53:52.545: E/AndroidRuntime(875):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
 05-23 02:53:52.545: E/AndroidRuntime(875):     ... 11 more
 05-23 02:54:33.206: E/AndroidRuntime(913): FATAL EXCEPTION: main
 05-23 02:54:33.206: E/AndroidRuntime(913): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intelsocket/com.example.intelsocket.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.os.Handler.dispatchMessage(Handler.java:99)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.os.Looper.loop(Looper.java:137)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.app.ActivityThread.main(ActivityThread.java:5041)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at java.lang.reflect.Method.invokeNative(Native Method)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at java.lang.reflect.Method.invoke(Method.java:511)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at dalvik.system.NativeStart.main(Native Method)
 05-23 02:54:33.206: E/AndroidRuntime(913): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button
 05-23 02:54:33.206: E/AndroidRuntime(913):     at com.example.intelsocket.MainActivity.set_btn_status(MainActivity.java:275)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at com.example.intelsocket.MainActivity.onCreate(MainActivity.java:62)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.app.Activity.performCreate(Activity.java:5104)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 05-23 02:54:33.206: E/AndroidRuntime(913):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
 05-23 02:54:33.206: E/AndroidRuntime(913):     ... 11 more

这里这个错误 我是这样解决的:

   public void set_btn_status(boolean paramBoolean)//使能按钮控件
   {
     ((Button)findViewById(2131230727)).setEnabled(paramBoolean);
     ((Button)findViewById(2131230728)).setEnabled(paramBoolean);
     ((Button)findViewById(2131230729)).setEnabled(paramBoolean);
     ((Button)findViewById(2131230730)).setEnabled(paramBoolean);
   }

改成:

 public void set_btn_status(boolean paramBoolean)//使能按钮控件
   {
     ((Button)findViewById(R.id.ledonBtn)).setEnabled(paramBoolean);
     ((Button)findViewById(R.id.ledoffBtn)).setEnabled(paramBoolean);
     ((Button)findViewById(R.id.jdqonBtn)).setEnabled(paramBoolean);
     ((Button)findViewById(R.id.jdqoffBtn)).setEnabled(paramBoolean);
   }
上一篇:系统间通信(5)——IO通信模型和JAVA实践 下篇


下一篇:PCA understanding