应用程序崩溃从另一个类调用意图启用蓝牙

时间:2016-03-04 10:42:20

标签: java android android-intent bluetooth

我是Java的新手,我试图建立蓝牙连接。我已经制作了一个蓝牙类来启用和禁用蓝牙,但是当我从另一个活动中调用蓝牙类时,它会崩溃。我认为这是导致崩溃的意图。

这是来自我的蓝牙课程(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);
    }

2 个答案:

答案 0 :(得分:0)

看起来你必须在oncreate()中使用getDefaultAdapter,所以试试这样希望这对你有用。

private BluetoothAdapter mBluetoothAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.frg_home, container, false);
    findViews(rootView);
    try {

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
        }
    } catch (Exception e) {
    }
    return rootView;
}

答案 1 :(得分:0)

嘿,我可以在这里看到一个主要问题

代码中的以下行

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

错误应该是

 Intent yourIntentname = new Intent(yourActivityOrServiceSource.this, yourActivityOrServiceDestination.class);

由于你的intent对象是null,并且你正在尝试用它做一些事情,因此你得到空指针引用。

发现了你的评论中的另一个问题

意图是在另一个类中创建的,这里是一个匿名内部类OnClickListener。因此,这并不是按照预期引用Activity(或Context)的实例,而是引用匿名内部类OnClickListener的实例。

所以你可以像

那样缓存这个对象

Classname = this;

并使用for intent

的变量

更新

如果您打算启用蓝牙

你可以使用

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter.enable();

相关问题