クラス本体自体で宣言されている ArrayAdapter を使用しているアクティビティがあります。
ArrayAdapter<String> btDevArrayAdapter = null;
私の onCreate() 関数では、次のことを行います。
ListView btDevList = (ListView)findViewById(R.id.btList);
ArrayAdapter<String> btDevArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
btDevList.setAdapter(btDevArrayAdapter);
btDevArrayAdapter.add("test");
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
if (btDevArrayAdapter==null) {
Log.d("ARRAY ADAPTER ON CREATE"," I AM NULL");
} else {
Log.d("ARRAY ADAPTER"," I AM INITIALIZED "); //this always shows up
}
後で、ブロードキャストレシーバーを登録します
registerReceiver(mReceiver, filter);
Bluetooth デバイスの検出を開始します
btAdapter.startDiscovery();
これBroadcastReceiver
は、クラス本体でも宣言されています。
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
if (btDevArrayAdapter==null) {
Log.d("ARRAY ADAPTER"," I AM NULL"); // <-- NullPointerException
} else {
if (device == null)
btDevArrayAdapter.add("was null!");
else
btDevArrayAdapter.add(device.getAddress());
}
}
}
};
私のlogcatを見ると、常に次のようになります。
01-01 02:34:02.200: D/ARRAY ADAPTER(1745): I AM INITIALIZED
01-01 02:34:05.393: D/ARRAY ADAPTER(1745): I AM NULL
ArrayAdapter が null になる原因は何ですか?