当我尝试在整个Android应用程序中使用Application对象维护蓝牙服务时,我的应用程序会抛出Classcast异常。当我尝试连接蓝牙外设时。
04-27 09:11:37.704 3289-3289/com.xemodex.bluetooth.xemodex E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xemodex.bluetooth.xemodex, PID: 3289
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xemodex.bluetooth.xemodex/com.xemodex.bluetooth.xemodex.DeviceControlActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.xemodex.bluetooth.xemodex.BluetoothController
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.xemodex.bluetooth.xemodex.BluetoothController
at com.xemodex.bluetooth.xemodex.DeviceControlActivity.onCreate(DeviceControlActivity.java:135)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method
)
答案 0 :(得分:0)
将BluetoothController设置为AndroidManifest.xml中的应用程序
<application
android:icon="@drawable/ic_launcher"
android:name=".BluetoothController" <-- Here is the change
android:label="@string/app_name"
android:logo="@drawable/ic_launcher"
android:theme="@android:style/Theme.Holo.Light" >