Android 蓝牙崩溃应用程序
Android bluetooth crashing application
使用 device.getName()
时遇到一些问题。由于某种原因导致我的应用程序崩溃。
我正在做的是在对话框中显示适配器(包含蓝牙名称)。当我 运行 应用程序并添加 device.getAdress()
而不是 - .getName()
时,应用程序工作正常,但是当使用 .getName()
时它崩溃了。
mBluetoothAdapter.startDiscovery();
receiver = 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);
toothadapter.add(device.getName());
toothadapter.notifyDataSetChanged();
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(bluetooth.this);
builder.setTitle("Nearby Devices:");
builder.setAdapter(toothadapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
AlertDialog alert = builder.create();
alert.show();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
真的需要一些帮助来了解为什么会这样。
上面的代码,都包含在 onCreate()
中 buttonListener
.
Really need some help as to why this is happening.
如果不查看堆栈跟踪,几乎不可能确定导致应用程序崩溃的原因。
但是请注意 BluetoothDevice.getName() returns null
如果 "there was a problem"。我们知道(从您收到的 Toast
通知)应用程序中的方法 returns null
s。这些 null
可能是也可能不是崩溃的原因。
另外,null
设备名不能造成NPE
的地方是ArrayAdapter
。当您调用 new ArrayAdapter<String>(Context, int)
时,将调用以下 constructor:
public ArrayAdapter(Context context, int resource) {
init(context, resource, 0, new ArrayList<T>());
}
用于存储数据的数据结构是 ArrayList
其 add()
's method implementation allows adding null
as opposed to the List<>
's add()
方法声明。
使用 device.getName()
时遇到一些问题。由于某种原因导致我的应用程序崩溃。
我正在做的是在对话框中显示适配器(包含蓝牙名称)。当我 运行 应用程序并添加 device.getAdress()
而不是 - .getName()
时,应用程序工作正常,但是当使用 .getName()
时它崩溃了。
mBluetoothAdapter.startDiscovery();
receiver = 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);
toothadapter.add(device.getName());
toothadapter.notifyDataSetChanged();
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(bluetooth.this);
builder.setTitle("Nearby Devices:");
builder.setAdapter(toothadapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
AlertDialog alert = builder.create();
alert.show();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
真的需要一些帮助来了解为什么会这样。
上面的代码,都包含在 onCreate()
中 buttonListener
.
Really need some help as to why this is happening.
如果不查看堆栈跟踪,几乎不可能确定导致应用程序崩溃的原因。
但是请注意 BluetoothDevice.getName() returns null
如果 "there was a problem"。我们知道(从您收到的 Toast
通知)应用程序中的方法 returns null
s。这些 null
可能是也可能不是崩溃的原因。
另外,null
设备名不能造成NPE
的地方是ArrayAdapter
。当您调用 new ArrayAdapter<String>(Context, int)
时,将调用以下 constructor:
public ArrayAdapter(Context context, int resource) {
init(context, resource, 0, new ArrayList<T>());
}
用于存储数据的数据结构是 ArrayList
其 add()
's method implementation allows adding null
as opposed to the List<>
's add()
方法声明。