Note that there are some explanatory texts on larger screens.

plurals
  1. POMy NFC app stop auomatically
    primarykey
    data
    text
    <p>I am a beginner of android application development. Currently I am trying to create an app which will launch after sensing the NFC tag, then it will read the tag the display the message in text view. It seems some problems exists causing my program stop unexpectedly.</p> <p>Here is my java file:</p> <pre><code>public class Reader extends Activity { TextView mText; NfcAdapter mAdapter; PendingIntent mPendingIntent; IntentFilter mFilters[]; String mTechLists[][]; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reader); mText = (TextView) findViewById(R.id.text); mAdapter = NfcAdapter.getDefaultAdapter(this); mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); try{ ndef.addDataType("text/plain"); }catch(MalformedMimeTypeException e){ throw new RuntimeException("fail", e); } IntentFilter nt = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); mFilters = new IntentFilter[]{ ndef, nt }; mTechLists = new String[][]{ new String[]{ Ndef.class.getName() } }; Intent intent = getIntent(); mText.setText(getNdefMessages(intent)); } public String getNdefMessages(Intent intent){ NdefMessage[] msgs = null; String action = intent.getAction(); if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)){ Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); if(rawMsgs != null){ msgs = new NdefMessage[rawMsgs.length]; for(int i=0; i&lt;rawMsgs.length; i++){ msgs[i] = (NdefMessage) rawMsgs[i]; } }else{ byte[] empty = new byte[]{}; NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty); NdefMessage msg = new NdefMessage(new NdefRecord[]{record}); msgs = new NdefMessage[]{msg}; } } else { finish(); } return msgs.toString(); } @Override public void onResume(){ super.onResume(); mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); } @Override public void onPause(){ super.onPause(); mAdapter.disableForegroundDispatch(this); } @Override public void onNewIntent(Intent intent){ Log.i("Foreground dispatch", "Discovered tag with intent:" + intent); mText = (TextView) findViewById(R.id.text); mText.setText(getNdefMessages(intent)); } } </code></pre> <p>And below is the AndroidManifest file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.reader" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="10" android:targetSdkVersion="15" /&gt; &lt;uses-permission android:name="android.permission.NFC"/&gt; &lt;uses-feature android:name="android.hardware.nfc" android:required="true"/&gt; &lt;application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme" android:debuggable="true"&gt; &lt;activity android:name=".Reader" android:label="@string/title_activity_reader"&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;data android:scheme="http" android:host="developer.android.com"/&gt; &lt;category android:name="android.intent.category.DEFAULT"/&gt; &lt;/intent-filter&gt; &lt;intent-filter&gt; &lt;action android:name="android.nfc.action.TAG_DISCOVERED"/&gt; &lt;category android:name="android.intent.category.DEFAULT"/&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>The tag I use to test is containing an url record. Can anyone give a hand to me? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload