2016-09-26 5 views
2

device.getName()을 사용하는 데 약간의 문제가 있습니다. 그것은 내 응용 프로그램을 충돌시키는 원인이됩니다.Android 블루투스 충돌 응용 프로그램

내가하고있는 일은 대화 상자에 어댑터 (블루투스 이름 보유)를 표시하는 것입니다. 응용 프로그램을 실행하고 - .getName() 대신 device.getAdress()을 추가하면 응용 프로그램이 제대로 작동하지만 .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); 

왜 이런 일이 발생하는지에 대한 도움이 필요합니다. 위의 코드는 buttonListener 내에 onCreate()에 모두 포함되어 있습니다.

+1

'toothadapter'의 유형은 무엇입니까? – Onik

+0

toothadapter = new ArrayAdapter (bluetooth.this, android.R.layout.select_dialog_item); –

+0

그게 문제의 주요 부분이고, 디버깅을 할 때 s7 가장자리를 잡을 수 없으며 기계가 가지고 있지 않기 때문에 bt를 에뮬레이트 할 수 없습니다.- –

답변

1

왜 이런 일이 일어나고 있는지에 대한 도움이 필요합니다.

stacktrace를 보지 않고도 응용 프로그램이 중단되는 원인을 확실히 말할 수는 없습니다.

그러나 참고 BluetoothDevice.getName() 반환 null 경우 "문제가 발생했습니다." 그리고 메소드에서 null을 앱에 반환한다는 알림을 받았습니다 ( Toast 알림이 있음). 이 null은 충돌의 원인 일 수도 있고 아닐 수도 있습니다.

또한 null 장치 이름이 NPE이 될 수없는 곳은 ArrayAdapter입니다. 당신이 new ArrayAdapter<String>(Context, int)를 호출 할 때 다음과 같은 constructor가 호출됩니다 : 데이터를 저장하는 데 사용

public ArrayAdapter(Context context, int resource) { 
    init(context, resource, 0, new ArrayList<T>()); 
} 

데이터 구조는 그 add()의 방법을 구현 List<>에 반대 null를 추가 할 수의 add() 메소드 선언 ArrayList입니다.

+0

@Rasmus Hjorth Lüdeking 충돌에 관한 새로운 정보가있을 때 답을 수정하겠습니다. – Onik