我是新來的Java和我試圖建立一個藍牙連接。我做了一個藍牙類來啓用和禁用藍牙,但是當我從另一個活動呼叫藍牙類時,它會崩潰。我認爲這是造成事故的意圖。應用程序崩潰從另一個類的調用意圖,以啓用藍牙
這是從我的藍牙Class(Bluetooth.java)
public class Bluetooth extends AppCompatActivity {
private final static int REQUEST_ENABLE_BT = 1;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
public void bt_Check(View v){
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isEnabled()) {
Snackbar.make(v, "BT is ON, now what?", Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(v, "Bluetooth is currently disabled", Snackbar.LENGTH_LONG)
.setAction("ENABLE BLUETOOTH", new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "Enabling bluetooth", Snackbar.LENGTH_LONG).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}).show();
}
} else {
Snackbar.make(v, "Device does not support Bluetooth", Snackbar.LENGTH_LONG).show();
}
}
的logcat:
03-04 11:45:17.104 23762-23762/com.hszuyd.noodle_.testing E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hszuyd.noodle_.testing, PID: 23762
Theme: themes:{}
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:3931)
at android.app.Activity.startActivityForResult(Activity.java:3890)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
at com.hszuyd.noodle_.testing.Bluetooth$1.onClick(Bluetooth.java:27)
at android.support.design.widget.Snackbar$2.onClick(Snackbar.java:295)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21156)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_app"
android:label="@string/app_name"
android:supportsRtl="false"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".KickPanelActivity"
android:label="@string/title_activity_kickpanel"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".TribotActivity"
android:label="@string/title_activity_tribot"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
這裏就是我從調用KickPanelActivity(我說的是活動):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kickpanel);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Replace this with something useful
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show(); //Action that should be run when the snackbar!? is pressed
}
});
}
public void button_bt_check_OnClick(View v) {
bluetooth.bt_Check(v);
}
你的錯誤日誌請 –
添加日誌(貓):-) – Remco1250
爲你的活動顯示清單,似乎有什麼錯誤的主題 – IgorB