我一直在尝试通过以下代码获取android中蓝牙的状态:我BluetoothAdapter
在方法中实例化......并像这样onCreate()
调用blue.isEnabled()
if(blue.isEnabled()){
....
}
但是我一直收到这个错误。是的,我已经声明了所有权限Bluetooth
错误日志
10-05 21:48:16.881: E/AndroidRuntime(538): FATAL EXCEPTION: main
10-05 21:48:16.881: E/AndroidRuntime(538): java.lang.NullPointerException
10-05 21:48:16.881: E/AndroidRuntime(538): at com.emazdoor.remind.me.main.onClick(main.java:58)
10-05 21:48:16.881: E/AndroidRuntime(538): at android.view.View.performClick(View.java:2485)
10-05 21:48:16.881: E/AndroidRuntime(538): at android.view.View$PerformClick.run(View.java:9080)
10-05 21:48:16.881: E/AndroidRuntime(538): at android.os.Handler.handleCallback(Handler.java:587)
10-05 21:48:16.881: E/AndroidRuntime(538): at android.os.Handler.dispatchMessage(Handler.java:92)
10-05 21:48:16.881: E/AndroidRuntime(538): at android.os.Looper.loop(Looper.java:123)
10-05 21:48:16.881: E/AndroidRuntime(538): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-05 21:48:16.881: E/AndroidRuntime(538): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 21:48:16.881: E/AndroidRuntime(538): at java.lang.reflect.Method.invoke(Method.java:507)
10-05 21:48:16.881: E/AndroidRuntime(538): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-05 21:48:16.881: E/AndroidRuntime(538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-05 21:48:16.881: E/AndroidRuntime(538): at dalvik.system.NativeStart.main(Native Method)
请帮忙!谢谢
完整代码
public class main extends Activity implements OnClickListener{
Button CallButton, SMSButton, BirthdayButton, BusinessButton;
BluetoothAdapter b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
b1 = BluetoothAdapter.getDefaultAdapter();
Initialiser();
}
private void Initialiser() {
CallButton = (Button) findViewById(R.id.CallButton);
SMSButton = (Button) findViewById(R.id.SMSButton);
BirthdayButton = (Button) findViewById(R.id.BirthdayButton);
BusinessButton = (Button) findViewById(R.id.BusinessButton);
CallButton.setOnClickListener(this);
SMSButton.setOnClickListener(this);
BirthdayButton.setOnClickListener(this);
BusinessButton.setOnClickListener(this);
}
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.CallButton:
startActivity(new Intent(this, CallRemind.class));
break;
case R.id.SMSButton:
Toast.makeText(this, "SMS Button", Toast.LENGTH_LONG).show();
startActivity(new Intent(this, SMSReminder.class));
break;
case R.id.BirthdayButton:
Toast.makeText(this, "Birthday Button", Toast.LENGTH_LONG).show();
b1.enable();
break;
case R.id.BusinessButton:
Toast.makeText(this, "Business Button", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.layout_main, menu);
return true;
}
}