由于Null指针异常,蓝牙应用程序无法运行。没有错误,但我无法看到连接到模拟器,因为应用程序每次都崩溃。即使我尝试GitHub示例代码,它也不会运行。我将发布代码和catlog以及您需要的任何其他内容。谢谢您的帮助
package com.example.bluetooth_demoproject;
import android.app.Activity;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.Set;
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_DISCOVER_BT_ = 1;
TextView mBluetoothStatus, mPairedDevicelist;
ImageView mBluetoothIcon;
Button mOnButton, mOffButton, mDiscoverableButton, mPairedDevices,
mSendImage;
BluetoothAdapter mBlueAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mBluetoothStatus = findViewById(R.id.BluetoothStatus);
mPairedDevicelist = findViewById(R.id.pairedDeviceList);
mBluetoothIcon = findViewById(R.id.bluetoothIcon);
mOnButton = findViewById(R.id.onButton);
mOffButton = findViewById(R.id.offButton);
mDiscoverableButton = findViewById(R.id.discoverableButton);
mPairedDevices = findViewById(R.id.pairedDevices);
mSendImage = findViewById(R.id.sendImage);
//adapter
BluetoothAdapter mBluetoothAdapter =
BluetoothAdapter.getDefaultAdapter();
// check if bluetooth is available
if(mBluetoothAdapter == null){
mBluetoothStatus.setText("Bluetooth is not available");
}
else {
mBluetoothStatus.setText("Bluetooth is available");
}
//if Bluetooth isnt enabled, enable it
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
//on button Click
mOnButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (!mBlueAdapter.isEnabled()) {
showToast("Turning Bluetooth on...");
// intent to on bluetooth
Intent intent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
}
else {
showToast("Bluetooth is already on");
}
}
});
//discover Bluetooth button
mDiscoverableButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (!mBlueAdapter.isDiscovering()) {
showToast("Discovering devices..");
Intent intent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(intent, REQUEST_DISCOVER_BT_);
}
}
});
// off button click
mOffButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBlueAdapter.isEnabled()) {
showToast("Turning Bluetooth off...");
// intent to turn off bluetooth
mBluetoothIcon.setImageResource(R.drawable.action_off);
}
else{
showToast("Bluetooth is already off");
}
}
});
//get paired device button click
Set<BluetoothDevice> pairedDevices =
mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
// Retrieve the name and address of the paired devices
for (BluetoothDevice device: pairedDevices) {
String deviceName = device.getName();
String deviceAddr = device.getAddress();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == RESULT_OK){
//bluetooth is on
mBluetoothIcon.setImageResource(R.drawable.action_on);
showToast("Bluetooth is on");
}
else {
//user is denied turning on Bluetooth
showToast("Unable to turn on Bluetooth");
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
//toast message function
private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT) .show();
}
}
9-08 22:03:46.371 1510-2830/system_process W/ActivityManager:
getRunningAppProcesses: caller 10060 does not hold REAL_GET_TASKS;
limiting output
09-08 22:03:46.372 5869-5869 /? I / InstantRun:启动即时运行服务器:是主进程09-08 22:03:46.416 5869-5869 /? E / BluetoothAdapter:蓝牙活页夹为空09-08 22:03:46.416 5869-5869 /? D / AndroidRuntime:关闭VM 09-08 22:03:46.418 5869-5869 /? E / AndroidRuntime:FATAL EXCEPTION:main进程:com.example.bluetooth_demoproject,PID:5869 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.bluetooth_demoproject / com.example.bluetooth_demoproject.MainActivity}:java.lang。 NullPointerException:尝试在android.app.ActivityThread.handleLaunchActivity(ActivityThread)的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)上的空对象引用上调用虚拟方法'boolean android.bluetooth.BluetoothAdapter.isEnabled()'。 java:2387)在Android.app.Handler.dispatchMessage(Handler.java:)的android.app.ActivityThread.access $ 800(ActivityThread.java:151)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 102)在android.app.Looper.loop(Looper.java:135)的android.app.ActivityThread.main(ActivityThread.java:5254)java.lang.reflect.Method.invoke(Native Method)at java.lang com.android.internal.os.ZygoteInit $ MethodAndArgsC中的.reflect.Method.invoke(Method.java:372) aller.run(ZygoteInit.java:903)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)引起:java.lang.NullPointerException:尝试调用虚方法'boolean android.bluetooth.BluetoothAdapter .isEnabled()'在android.app.Instrumentation.callActivityOnCreate的android.app.Activity.performCreate(Activity.java:5990)的com.example.bluetooth_demoproject.MainActivity.onCreate(MainActivity.java:57)的空对象引用上(Instrumentation.java:1106)android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)android.app.ActivityThread.access $ 800(ActivityThread.java) :151)在android.app.Loper.loop(Looper.java:135)的android.app.Handler.dispatchMessage(Handler.java:102)上的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)在java.la的java.lang.reflect.Method.invoke(Native Method)的android.app.ActivityThread.main(ActivityThread.java:5254)在com.android.internal.os.ZygoteInit.main(ZygoteInit。)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)中的ng.reflect.Method.invoke(Method.java:372)。 java:698)09-08 22:03:46.431 1510-2232 / system_process W / ActivityManager:强制完成活动1 com.example.bluetooth_demoproject / .MainActivityenter code here
看起来问题来自第59行:if (!mBluetoothAdapter.isEnabled()) {
注意:错误消息显示第57行,但在我的文本编辑器中显示为59。
上面几行,你检查对象mBluetoothAdapter
是否成功创建,检查它是否是== null
。但在那之后,你称它为方法:.isEnabled()
。我认为问题是mBluetoothAdapter
正在成为null
,因此当您尝试访问其中一个方法时会抛出NullPointerException。
如果我理解正确,我认为解决方案将是这样的:
// check if bluetooth is available
if(mBluetoothAdapter == null){
mBluetoothStatus.setText("Bluetooth is not available");
}
else {
mBluetoothStatus.setText("Bluetooth is available");
//if Bluetooth isnt enabled, enable it
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
这将使启用代码远离mBluetoothAdapter
,其值为null
。
另外,如果BlueTooth不可用,您可以使用return
退出方法。
// check if bluetooth is available
if(mBluetoothAdapter == null){
mBluetoothStatus.setText("Bluetooth is not available");
return;
}
else {
mBluetoothStatus.setText("Bluetooth is available");
}
//if Bluetooth isnt enabled, enable it
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
这样做会以一种更清洁的方式做同样的事情。
我认为你的下一步是找出为什么BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
正在返回null
而不是你想要的对象。我不认为答案就在你的MainActivity
方法中。设备找不到默认适配器,这可能是许多问题的结果。