1

アクティビティに 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

4

1 に答える 1

3

これは、 http://developer.android.com/reference/android/nfc/NfcAdapter.html#getDefaultAdapter(android.content.Context)getDefaultAdapterで説明されているように、携帯電話に NFC がない場合、メソッドが null を返すためです。

于 2013-06-14T09:19:21.133 に答える