我一直在試圖通過這個代碼,以獲得Android的藍牙的狀態: 我onCreate()
方法實例化BluetoothAdapter
...並呼籲blue.isEnabled()
這樣如何在android中開啓/關閉藍牙?
if(blue.isEnabled()){
....
}
但我一直得到這個error..and是我已經宣佈所有權限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;
}
}
@ user1281750確定我已經更新了代碼。 –