Java 由于空指针错误,Android Bluetooth应用程序无法运行

Java 由于空指针错误,Android Bluetooth应用程序无法运行,java,android,android-studio,bluetooth,Java,Android,Android Studio,Bluetooth,由于空指针异常,蓝牙应用程序无法运行。没有错误,但我看不到如何连接到模拟器,因为每次应用程序都会崩溃。即使我尝试示例GitHub代码,它也不会运行。我会发布代码和catlog以及你需要的任何东西。谢谢你的帮助 package com.example.bluetooth_demoproject; import android.app.Activity; import android.bluetooth.BluetoothA2dp; import android.bluetooth.Bluetoo

由于空指针异常,蓝牙应用程序无法运行。没有错误,但我看不到如何连接到模拟器,因为每次应用程序都会崩溃。即使我尝试示例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
package com.example.bluetooth\u演示项目;
导入android.app.Activity;
导入android.bluetooth.BluetoothA2dp;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.w3c.dom.Text;
导入java.util.Set;
公共类MainActivity扩展了活动{
私有静态最终整数请求\u启用\u BT=0;
私有静态最终整数请求\u DISCOVER\u BT=1;
文本视图mBluetoothStatus,mPairedDevicelist;
图像视图图标;
按钮mOnButton、mOffButton、mDiscoverableButton、mPairedDevices、,
mSendImage;
蓝牙适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
java.text.DateFormat DateFormat=android.text.format.DateFormat.getDateFormat(getApplicationContext());
mBluetoothStatus=findviewbyd(R.id.BluetoothStatus);
mPairedDevicelist=findViewById(R.id.pairedDeviceList);
mBluetoothIcon=findviewbyd(R.id.bluetoothIcon);
mOnButton=findViewById(R.id.onButton);
mOffButton=findviewbyd(R.id.offButton);
mDiscoverableButton=findViewById(R.id.discoverableButton);
mPairedDevices=findViewById(R.id.pairedDevices);
mSendImage=findViewById(R.id.sendImage);
//适配器
蓝牙适配器mBluetoothAdapter=
BluetoothAdapter.getDefaultAdapter();
//检查蓝牙是否可用
if(mBluetoothAdapter==null){
mBluetoothStatus.setText(“蓝牙不可用”);
}
否则{
mBluetoothStatus.setText(“蓝牙可用”);
}
//如果蓝牙未启用,请启用它
如果(!mBluetoothAdapter.isEnabled()){
Intent enableBtIntent=新建
意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
//点击按钮
mOnButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(!mBlueAdapter.isEnabled()){
showToast(“打开蓝牙…”);
//打算使用蓝牙
意图=新的
意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(意图、请求和启用);
}
否则{
showToast(“蓝牙已开启”);
}
}
});
//发现蓝牙按钮
MDisColverableButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(!mBlueAdapter.isDiscovering()){
showToast(“发现设备…”);
意图=新的
意图(BluetoothAdapter.ACTION\u REQUEST\u DISCOVERABLE);
startActivityForResult(意图、请求、发现、测试);
}
}
});
//关闭按钮单击
mOffButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(mBlueAdapter.isEnabled()){
showToast(“关闭蓝牙…”);
//想要关闭蓝牙吗
mBluetoothIcon.setImageResource(R.drawable.action_off);
}
否则{
showToast(“蓝牙已关闭”);
}
}
});
//获取配对设备按钮单击
设置pairedDevices=
mBluetoothAdapter.getBondedDevices();
如果(pairedDevices.size()>0){
//检索配对设备的名称和地址
用于(蓝牙设备:pairedDevices){
字符串deviceName=device.getName();
字符串deviceAddr=device.getAddress();
}
}
}
@凌驾
ActivityResult上受保护的void(int requestCode、int resultCode、Intent
(数据){
开关(请求代码){
案例请求\u启用\u BT:
if(resultCode==RESULT\u OK){
//蓝牙已打开
mBluetoothIcon.setImageResource(R.drawable.action_on);
showToast(“蓝牙开启”);
}
否则{
//拒绝用户打开蓝牙
showToast(“无法打开蓝牙”);
}
打破
}
super.onActivityResult(请求代码、结果代码、数据);
}
//toast消息函数
私有void showtoos(字符串msg){
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}
}
9-08 22:03:46.371 1510-2830/带活动管理器的系统/流程:
getRunningAppProcesses:调用方10060不持有实际的\u GET\u任务;
极限输出
09-08 22:03:46.3725869-5869/?I/InstantRun:启动即时运行服务器:是主进程 09-08 22:03:46.416 5869-5869/? E/BluetoothAdapter:蓝牙绑定器为空 09-08 22:03:46.416 5869-5869/? D/AndroidRuntime:关闭虚拟机 09-08 22:03:46.418 5869-5869/? E/AndroidRuntime:致命异常:主 流程:com.example.bluetooth\u demoproject,PID:5869 java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.bluetooth_demoproject/com.example.bluetooth_demoproject.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean android.bluetooth.BluetoothAdapter.isEnabled() 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)上 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:23
// 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);
    }
}
// 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);
}