扫描蓝牙设备时出错
getting error on scanning bluetooth devices
我正在执行扫描附近蓝牙设备的任务,但我不知道它在哪里出错,因为有时它运行顺利但有时它会出现错误,如下所示
09-09 16:46:49.574 5577-5577/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: aiyaz.conceptioni.ticket, PID: 5577
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2341)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1273)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:16845)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1621)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1199)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:609)
at android.view.View.measure(View.java:16845)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:824)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:500)
at android.view.View.measure(View.java:16845)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
at android.view.View.measure(View.java:16845)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1621)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:742)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:607)
at android.view.View.measure(View.java:16845)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2368)
at android.view.View.measure(View.java:16845)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2289)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1355)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1552)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1232)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6271)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5347)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
at dalvik.system.NativeStart.main(Native Method)
我不知道它从哪里得到 NullPointerException。我一直在寻找一整天,但找不到。这是我的代码
public class AppSettings extends Activity {
Button btnLogout;
ImageView imgBlutoothOnOff,homeIcon;
boolean isBluetoothOn;
BluetoothAdapter bluetoothAdapter;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
SharedPreferences sharedPreferences;
ListView bluetoothDeviceList;
ArrayList<String> bluetoothDeviceName=new ArrayList<>();
ArrayList<String> bluetoothDeviceId=new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
BluetoothDevices getBluetoothDevices;
CustomProgressDialog customProgressDialog;
private BroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.activity_app_settings);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
initialize();
imgBlutoothOnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isBluetoothOn){
bluetoothAdapter.disable();
isBluetoothOn=false;
imgBlutoothOnOff.setImageResource(R.drawable.bluetooth_key_off);
}else{
bluetoothAdapter.enable();
isBluetoothOn=true;
imgBlutoothOnOff.setImageResource(R.drawable.bluetooth_key_on);
}
}
});
}
public void initialize(){
btnLogout=(Button)findViewById(R.id.setting_logout_btn);
imgBlutoothOnOff=(ImageView)findViewById(R.id.settings_bluetooth_on_off);
databaseHelper=new DatabaseHelper(AppSettings.this);
sqLiteDatabase=databaseHelper.getWritableDatabase();
sharedPreferences=getSharedPreferences(ReferenceStrings.SharedPref,MODE_PRIVATE);
customProgressDialog=new CustomProgressDialog(AppSettings.this);
customProgressDialog.setCancelable(true);
bluetoothDeviceList=(ListView)findViewById(R.id.app_settings_bluetooth_list);
if(bluetoothAdapter.isEnabled()){
isBluetoothOn=true;
imgBlutoothOnOff.setImageResource(R.drawable.bluetooth_key_on);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver_search, filter);
startDiscovery();
}
else {
isBluetoothOn = false;
imgBlutoothOnOff.setImageResource(R.drawable.bluetooth_key_off);
}
}
private void getPairedDevices(){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
List<String> s = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices) {
s.add(bt.getName());
}
startDiscovery();
}
private void startDiscovery(){
arrayAdapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,bluetoothDeviceName);
arrayAdapter.setNotifyOnChange(true);
bluetoothDeviceList.setAdapter(arrayAdapter);
bluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive (Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
arrayAdapter.clear();
arrayAdapter.notifyDataSetChanged();
Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_LONG).show();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
break;
case BluetoothAdapter.STATE_ON:
Toast.makeText(context, "Bluetooth is on", Toast.LENGTH_LONG).show();
startDiscovery();
break;
case BluetoothAdapter.STATE_TURNING_ON:
break;
}
}
}
};
final BroadcastReceiver mReceiver_search = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
customProgressDialog.show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
customProgressDialog.cancel();
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//bluetooth device found
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
bluetoothDeviceName.add(device.getName());
bluetoothDeviceId.add(device.getAddress());
arrayAdapter.notifyDataSetChanged();
}
}
};
@Override
protected void onPause() {
super.onPause();
try {
if(mReceiver!=null) {
unregisterReceiver(mReceiver);
}
}catch (Exception e){
}
}
}
请忽略未使用的变量。如果我做错了什么,请告诉我。谢谢你。 :)
请注意,找到的蓝牙设备名称可能为空。因此,如果您在适配器中使用 bluetoothDeviceName.add(device.getName());
,请检查它。
我正在执行扫描附近蓝牙设备的任务,但我不知道它在哪里出错,因为有时它运行顺利但有时它会出现错误,如下所示
09-09 16:46:49.574 5577-5577/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: aiyaz.conceptioni.ticket, PID: 5577
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2341)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1273)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:16845)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1621)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1199)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:609)
at android.view.View.measure(View.java:16845)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:824)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:500)
at android.view.View.measure(View.java:16845)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
at android.view.View.measure(View.java:16845)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1621)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:742)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:607)
at android.view.View.measure(View.java:16845)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2368)
at android.view.View.measure(View.java:16845)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2289)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1355)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1552)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1232)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6271)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5347)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
at dalvik.system.NativeStart.main(Native Method)
我不知道它从哪里得到 NullPointerException。我一直在寻找一整天,但找不到。这是我的代码
public class AppSettings extends Activity {
Button btnLogout;
ImageView imgBlutoothOnOff,homeIcon;
boolean isBluetoothOn;
BluetoothAdapter bluetoothAdapter;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
SharedPreferences sharedPreferences;
ListView bluetoothDeviceList;
ArrayList<String> bluetoothDeviceName=new ArrayList<>();
ArrayList<String> bluetoothDeviceId=new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
BluetoothDevices getBluetoothDevices;
CustomProgressDialog customProgressDialog;
private BroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.activity_app_settings);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
initialize();
imgBlutoothOnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isBluetoothOn){
bluetoothAdapter.disable();
isBluetoothOn=false;
imgBlutoothOnOff.setImageResource(R.drawable.bluetooth_key_off);
}else{
bluetoothAdapter.enable();
isBluetoothOn=true;
imgBlutoothOnOff.setImageResource(R.drawable.bluetooth_key_on);
}
}
});
}
public void initialize(){
btnLogout=(Button)findViewById(R.id.setting_logout_btn);
imgBlutoothOnOff=(ImageView)findViewById(R.id.settings_bluetooth_on_off);
databaseHelper=new DatabaseHelper(AppSettings.this);
sqLiteDatabase=databaseHelper.getWritableDatabase();
sharedPreferences=getSharedPreferences(ReferenceStrings.SharedPref,MODE_PRIVATE);
customProgressDialog=new CustomProgressDialog(AppSettings.this);
customProgressDialog.setCancelable(true);
bluetoothDeviceList=(ListView)findViewById(R.id.app_settings_bluetooth_list);
if(bluetoothAdapter.isEnabled()){
isBluetoothOn=true;
imgBlutoothOnOff.setImageResource(R.drawable.bluetooth_key_on);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver_search, filter);
startDiscovery();
}
else {
isBluetoothOn = false;
imgBlutoothOnOff.setImageResource(R.drawable.bluetooth_key_off);
}
}
private void getPairedDevices(){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
List<String> s = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices) {
s.add(bt.getName());
}
startDiscovery();
}
private void startDiscovery(){
arrayAdapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,bluetoothDeviceName);
arrayAdapter.setNotifyOnChange(true);
bluetoothDeviceList.setAdapter(arrayAdapter);
bluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive (Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
arrayAdapter.clear();
arrayAdapter.notifyDataSetChanged();
Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_LONG).show();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
break;
case BluetoothAdapter.STATE_ON:
Toast.makeText(context, "Bluetooth is on", Toast.LENGTH_LONG).show();
startDiscovery();
break;
case BluetoothAdapter.STATE_TURNING_ON:
break;
}
}
}
};
final BroadcastReceiver mReceiver_search = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
customProgressDialog.show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
customProgressDialog.cancel();
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//bluetooth device found
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
bluetoothDeviceName.add(device.getName());
bluetoothDeviceId.add(device.getAddress());
arrayAdapter.notifyDataSetChanged();
}
}
};
@Override
protected void onPause() {
super.onPause();
try {
if(mReceiver!=null) {
unregisterReceiver(mReceiver);
}
}catch (Exception e){
}
}
}
请忽略未使用的变量。如果我做错了什么,请告诉我。谢谢你。 :)
请注意,找到的蓝牙设备名称可能为空。因此,如果您在适配器中使用 bluetoothDeviceName.add(device.getName());
,请检查它。