我搜索过,但没有发现任何符号。我正在Android中使用蓝牙开发两种定义之间的连接。在ACTION_FOUND方法上,当我试图让设备信息出现在listView上时,我在logcat上得到了这个错误。
DesafioActivity: 公共类DesafioActivity扩展了Activity {
private TextView nomeDispositivo;
private TextView MAC_Adress;
private BluetoothAdapter bthAdapter = BluetoothAdapter.getDefaultAdapter();
private ArrayAdapter<String> bthDispositivosArea;
private ListView lv_devicesArea;
private IntentFilter filter;
private Button btn_servidor, btn_cliente;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_desafio);
nomeDispositivo = (TextView)findViewById(id.tv_apresentaNomeDispositivo);
MAC_Adress = (TextView)findViewById(id.tv_apresentaMacAdress);
lv_devicesArea = (ListView)findViewById(id.lv_apresentaDispositivosDescobertos);
VerificarEstadoBth();
nomeDispositivo.setText(bthAdapter.getName());
MAC_Adress.setText(bthAdapter.getAddress());
//Regista o BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(HandleDiscovery, filter); // Don't forget to unregister during onDestroy
bthAdapter.startDiscovery();
}
public void VerificarEstadoBth(){
//Bluetooth não suportado
if (bthAdapter == null) {
Toast.makeText(this, "DEVIDE DOESN'T SUPPORT", Toast.LENGTH_LONG).show();
}
// Ativa e torna o dispositivo visível para conexão
if (!bthAdapter.isDiscovering()) {
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
Toast.makeText(getApplicationContext(),"AVAIABLE FOR CONNECTION" ,Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"AVAIABLE FOR CONNECTION",Toast.LENGTH_SHORT).show();
}
}
private final BroadcastReceiver HandleDiscovery = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(getApplicationContext(),"DEVICE FOUND! " ,Toast.LENGTH_LONG).show();
bthDispositivosArea.add("\n Device: " + device.getName() + ", " + device);
lv_devicesArea.setAdapter(bthDispositivosArea);
} else {
if(BluetoothDevice.ACTION_UUID.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
for (int i=0; i<uuidExtra.length; i++) {
//out.append("\n Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString());
}
} else {
if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Toast.makeText(getApplicationContext(),"START DISCOVERY!",Toast.LENGTH_SHORT).show();
}
}
}
}
};
@Override
public void onDestroy() {
unregisterReceiver(HandleDiscovery);
super.onDestroy();
}
}
logcat的:
12-30 16:33:24.657: E/AndroidRuntime(12794): FATAL EXCEPTION: main
12-30 16:33:24.657: E/AndroidRuntime(12794): Process: com.AMOV.mr.fit, PID: 12794
12-30 16:33:24.657: E/AndroidRuntime(12794): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } in com.AMOV.mr.fit.DesafioActivity$1@41a98b68
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:776)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.os.Handler.handleCallback(Handler.java:733)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.os.Handler.dispatchMessage(Handler.java:95)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.os.Looper.loop(Looper.java:136)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.app.ActivityThread.main(ActivityThread.java:5146)
12-30 16:33:24.657: E/AndroidRuntime(12794): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 16:33:24.657: E/AndroidRuntime(12794): at java.lang.reflect.Method.invoke(Method.java:515)
12-30 16:33:24.657: E/AndroidRuntime(12794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
12-30 16:33:24.657: E/AndroidRuntime(12794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
12-30 16:33:24.657: E/AndroidRuntime(12794): at dalvik.system.NativeStart.main(Native Method)
12-30 16:33:24.657: E/AndroidRuntime(12794): Caused by: java.lang.NullPointerException
12-30 16:33:24.657: E/AndroidRuntime(12794): at com.AMOV.mr.fit.DesafioActivity$1.onReceive(DesafioActivity.java:112)
12-30 16:33:24.657: E/AndroidRuntime(12794): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:766)
12-30 16:33:24.657: E/AndroidRuntime(12794): ... 9 more
答案 0 :(得分:4)
bthDispositivosArea,您的ArrayAdapter为null,因为您尚未在任何位置初始化它。而你正在尝试向null对象添加内容。