【问题标题】:app crashing on calling intent from another class to enable bluetooth应用程序在从另一个类调用意图以启用蓝牙时崩溃
【发布时间】:2016-03-04 10:42:20
【问题描述】:

我是 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);
    }

【问题讨论】:

  • 请您的错误日志
  • 添加了日志(猫):-)
  • 显示清单为您的活动,主题似乎有问题
  • Bluetooth.java 是你的班级吗?请张贴。
  • 当蓝牙不在单独的类中时它工作得很好。 (顺便添加了 androidmanifest)。

标签: java android android-intent bluetooth


【解决方案1】:

看来您必须在 oncreate() 中获取默认适配器,所以请尝试这样,希望这对您有用。

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;
}

【讨论】:

    【解决方案2】:

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

    代码中的以下行

    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    

    错了应该是

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

    由于您的意图对象为空,并且您正试图用它做一些事情,因此您得到空指针引用。

    从您的 cmets 中发现另一个问题

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

    所以你可以像缓存这个对象

    classname that = this;

    并使用用于意图的变量

    更新

    如果您只想启用蓝牙

    你可以使用

    蓝牙适配器 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter.enable();

    【讨论】:

    • 嘿,我试过这样:Intent enableBtIntent = new Intent(Bluetooth.this, KickPanelActivity.class); Bluetooth.this 是蓝牙类,而 KickPanelActivity.class 是我从中调用它的活动。
    • @Remco1250,你能在这里发布蓝牙课程吗
    • @Remco1250,明白了,这不是蓝牙。这只是新的 Intent(this, ActivityTwo.class)
    • @aravind.udayashankara 它没有构建:(
    • @aravind.udayashankara Error:(26, 33) error: no suitable constructor found for Intent(&lt;anonymous OnClickListener&gt;,Class&lt;KickPanelActivity&gt;) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; &lt;anonymous OnClickListener&gt; cannot be converted to String) constructor Intent.Intent(Context,Class&lt;?&gt;) is not applicable (argument mismatch; &lt;anonymous OnClickListener&gt; cannot be converted to Context)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    相关资源
    最近更新 更多