我试图让我的android在注册蓝牙连接时启动一个活动,我拥有所有适当的权限,并且这段代码运行良好,直到它需要启动意图。之后它崩溃了。现在我没有一个logcat,因为它在我的手机上进行实时测试。
那么,让我们再玩一个“我该如何解决这个问题?”的游戏。
public class detectService extends Service {
public void onCreate() {
IntentFilter filter1 = new IntentFilter(
BluetoothDevice.ACTION_ACL_CONNECTED);
this.registerReceiver(mReceiver, filter1);
Toast.makeText(this, "Starting Service!", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
// The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothAdapter device = intent
.getParcelableExtra(BluetoothDevice.ACTION_ACL_CONNECTED);
if (device.equals(action)) {
// Device is now connected
// Start Second Activity
Intent otherIntent = new Intent(detectService.this,
otherClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(otherIntent);
}
};
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
感谢这里有人的帮助,我的手机上现在有了 logcat!这是错误的副本:
E/AndroidRuntime( 567): FATAL EXCEPTION: main
E/AndroidRuntime( 567): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.ACL_CONNECTED (has extras) } in lionsimages.com.bluetoothalert.detectService$1@4051ad68
E/AndroidRuntime( 567): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
E/AndroidRuntime( 567): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 567): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 567): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 567): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 567): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 567): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 567): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
E/AndroidRuntime( 567): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
E/AndroidRuntime( 567): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 567): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 567): at lionsimages.com.bluetoothalert.detectService$1.onReceive(detectService.java:39)
E/AndroidRuntime( 567): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
E/AndroidRuntime( 567): ... 9 more