gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:13:58 25 4
gpt4 key购买 nike

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

我有一个单独的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com