アクティビティに NFC フォアグラウンド ディスパッチを実装しました。Android 4.2.1デバイス (Samsung Galaxy Nexus)で実行すると、コードは正常に動作します。しかし、Android 2.3.5デバイス (HTC Desire S) で実行すると、NullPointerExceptionが発生します。これが私のアクティビティのコードです。例外がonResume()
部分的にスローされます。
public class MainActivity extends Activity{
private NfcAdapter mAdapter;
private IntentFilter[] intentFilterArray;
private PendingIntent pendingIntent;
private String[][] techArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAdapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
intentFilterArray = new IntentFilter[]{intentFilter};
techArray = new String[][]{new String[]{NdefFormatable.class.getName(), NfcA.class.getName()}};
}
@Override
protected void onResume(){
super.onResume();
//NullPointerException here,because mAdapter is Null, why?
mAdapter.enableForegroundDispatch(this, pendingIntent, intentFilterArray, techArray);
}
@Override
public void onPause() {
super.onPause();
mAdapter.disableForegroundDispatch(this);
}
...
}
Android 2.3.5デバイスでは (LogCat に表示されます) when callでNullPointerExceptionが発生します。確認しましたが、それはnullです。なんで ?onResume()
mAdapter.enableForegroundDispatch(…)
mAdapter