title: NFC app crashing on enableForegroundDispatch
body: <p>I am trying to get NFC foreground dispatch to work in my app using the tutorials
  <a href="http://code.tutsplus.com/tutorials/reading-nfc-tags-with-android--mobile-17278" rel="nofollow">here</a> and <a href="http://www.survivingwithandroid.com/2015/03/nfc-in-android-ndef.html" rel="nofollow">here</a>. From what I can deduce all other functions seem to be working a it is just the NFC forground dispatch system that isn't working.</p>
  
  <p>In my onCreate, I check NFC exists and initialise pending Intents and filters and get the NFC adapter:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      //Set some stuff
      mTV = (TextView) findViewById(R.id.textView);
      mButt = (Button) findViewById(R.id.button);
      //mNfcAd = NfcAdapter.getDefaultAdapter(this);
      //Init elsewhere
      getAdapter();
  
      //Hide button until its needed
      mButt.setVisibility(View.INVISIBLE);
  
      //Make sure NFC actually exists.....
      if (mNfcAd == null) {
          //Not going to work without NFC
          Toast.makeText(this, "This device does not support NFC\nGet used to the pen and paper for now :/", Toast.LENGTH_LONG).show();
          finish();
          return;
      }
  
      Intent nfcIntent = new Intent(this, getClass());
      nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  
      nfcPendingIntent =
              PendingIntent.getActivity(this, 0, nfcIntent, 0);
  
      IntentFilter tagIntentFilter =
              new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
      try {
          tagIntentFilter.addDataType("text/plain");
          intentFiltersArray = new IntentFilter[]{tagIntentFilter};
      }
      catch (Throwable t) {
          t.printStackTrace();
      }
  
      //Init the prefs
      initPrefs();
  
  }
  
  public void initPrefs() {
      prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  }
  
  private NfcAdapter getAdapter(){
      if(mNfcAd ==null){
          NfcManager manager = (NfcManager) getSystemService(NFC_SERVICE);
          mNfcAd = manager.getDefaultAdapter();
      }
      return mNfcAd;
  }
  </code></pre>
  
  <p>I then go on to check NFC is enabled (if not display a button that shows NFC settings) as well as checking if the app is in its first run. At the end of onResume(), I (attempt to) enable Foreground Dispatch for the adapter.</p>
  
  <pre><code>protected void onResume() {
      super.onResume();
  
      //Define the mButt action
      mButt.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent dialogIntent = new Intent(Settings.ACTION_NFC_SETTINGS);
              dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(dialogIntent);
          }
      });
              //If NFC exists, check if its turned on
      if (!mNfcAd.isEnabled()) {
          Toast.makeText(this, "You have NFC, but you haven't enabled it...", Toast.LENGTH_LONG).show();
          mTV.setText("NFC not enabled");
          mButt.setVisibility(View.VISIBLE);
      }
      //Check if first run
      if (prefs.getInt("firstTime", 1) == 1 ||
              prefs.getString("fullName", null) == null ||
              prefs.getString("house", null) == null) {
          Intent intent = new Intent(this, First_Run.class);
          startActivity(intent);
          prefs.edit().putInt("firstTime", 0);
          prefs.edit().putString("fullName", getIntent().getExtras().getString("fullName"));
          prefs.edit().putString("house", getIntent().getExtras().getString("house"));
          prefs.edit().apply();
      }
      getAdapter().enableForegroundDispatch(this, nfcPendingIntent, intentFiltersArray, null);
      handleIntent(getIntent());
  }
  </code></pre>
  
  <p>The error is:</p>
  
  <pre><code>Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.nfc.NfcAdapter.enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][])' on a null object reference
  </code></pre>
  
  <p>However, all the arguments seem to be initialised;<br>
  It is being called in onResume, so the activity context is created;<br>
  nfcPendingIntent and intentFiltersArray are initialised in onCreate;<br>
  I do not have a techlist so I use null as the final argument;<br>
  The adapter itself is initialised in getAdapter.</p>
  
  <p>I am currently combing through other tutorials to see the different methods that can be used, however I would really like to get this fixed.</p>
  
  <p>Manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;uses-feature
      android:name="android.hardware.nfc"
      android:required="true" /&gt;
  
  &lt;uses-permission android:name="android.permission.NFC" /&gt;
  
  &lt;application
      android:fullBackupContent="false"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme"&gt;
  
      &lt;activity
          android:name=".MainActivity"
          android:label="@string/app_name"&gt;
  
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
  
          &lt;intent-filter&gt;
              &lt;action android:name="android.nfc.action.NDEF_DISCOVERED" /&gt;
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;data android:mimeType="text/plain" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;activity
          android:name=".First_Run"
          android:label="@string/title_activity_first__run"&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>