java - Android Studio蓝牙:- attempt to invoke a virtual method on a null object reference

标签 java android error-handling bluetooth

我正在构建一个简单的应用程序,它可以打开设备的蓝牙并将其设置为可见。

我有一个单独的java类文件,其中包含我需要的蓝牙功能,并且这些函数是通过该类的对象从链接到我的 Activity 的另一个java类调用的。

这是我的代码:

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by mark on 11/11/2016.
 */

public class Bluetooth_API extends AppCompatActivity{

    BluetoothAdapter blueAdp;

    public Bluetooth_API() {
        blueAdp = BluetoothAdapter.getDefaultAdapter();
    }

    protected int bluetooth_ON() {
        startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 0);
        //blueAdp.enable(); //instead of above line - without alert dialog for permission
        return 0;
    }

    protected int bluetooth_OFF() {
        blueAdp.disable(); //
        return 0;
    }

    protected int bluetooth_setVisible() {
        if(!blueAdp.isDiscovering()) {
            startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE), 0);
        }
        return 0;
    }

}

这是调用我的函数的其他 Activity 的代码部分:

 scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent nextLayout = new Intent(getApplicationContext(), com.ai.mark.robot_dancing.Scanning_Devices.class);
                startActivity(nextLayout);
                blue.bluetooth_ON();
                //blue.bluetooth_setVisible();
            }
        });

运行代码后,我收到以下错误,我相信这与 Activity 不正确有关,因为我的蓝牙功能位于另一个文件中(我还尝试将方法复制到我的 Activity 类中,它们工作得很好)。

错误:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.ai.mark.robot_dancing, PID: 21314 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:3951) at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77) at android.app.Activity.startActivityForResult(Activity.java:3912) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) at com.ai.mark.robot_dancing.Bluetooth_API.bluetooth_ON(Bluetooth_API.java:20) at com.ai.mark.robot_dancing.Bluetooth_Panel$6.onClick(Bluetooth_Panel.java:146) at android.view.View.performClick(View.java:5210) at android.view.View$PerformClick.run(View.java:21328) 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:5551) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

对于造成这种情况的原因有什么想法吗? 谢谢。

最佳答案

不要在Activity构造函数中调用此类代码:

blueAdp = BluetoothAdapter.getDefaultAdapter();

使用onCreate(android.os.Bundle)为此:

@Override
protected void onCreate(Bundle savedInstanceState) {
    blueAdp = BluetoothAdapter.getDefaultAdapter();
}

关于java - Android Studio蓝牙:- attempt to invoke a virtual method on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40607700/

相关文章:

java - 从左边框滑动以打开 Activity

php - 如何在 PHP 中获得有用的错误消息?

php - 我如何查看多个文件上传PHP中数组是否为空

c# - 模态弹出错误阻止MDI父GUI更新

java - 应用程序的客户端-服务器架构

java - 无法部署eclipse导出的war文件

android - Android 中的 getGroupIdLevel1()

android - 在三星机器人上拍摄的图库图像即使是纵向拍摄也始终是横向的

java - 两个 MQTT 连接同一设备

java - 无法提取从 Java 创建的 zip 文件