0

I have been doing some NFC development on android, and I have hit a brick wall. I launch the app and it immediatly fails with a "Unfortunately,..... has stopped working".

Some quick googling showed I need the put the permission in the manifest. I did that, but lo and behold, I still get the same error!

Here is the start of my manifest:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="10"/>
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

    <activity
        android:name=".MainActivity"
    <!-- rest of the file --->

And the output in logcat:

java.lang.RuntimeException: Unable to resume activity {za.co.fnb.ms.nfcreader/za.co.fnb.ms.nfcreader.MainActivity}: java.lang.SecurityException: NFC permission required: Neither user 10253 nor current process has android.permission.NFC.

What am I doing wrong here?

1
  • 3
    I think You have to declare the uses permission at the top of the Manifest, outside the application tag. Commented Sep 15, 2014 at 11:50

3 Answers 3

3

<uses-permission>, <uses-sdk>, and <uses-feature> go outside the <application> element:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.commonsware.android.jimmyb"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.NFC"/>

    <uses-feature
        android:name="android.hardware.nfc"
        android:required="true"/>

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter android:label="@string/app_name">
                <action android:name="android.nfc.action.NDEF_DISCOVERED"/>

                <data android:mimeType="vnd.secret/agent.man"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

(from this sample project)

2

The section

<uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="10"/>
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

Needs to be outside of your application tag.


Like

<uses-permission android:name="android.permission.NFC" />
<uses-sdk android:minSdkVersion="10"/>
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
...

Read Structure of the Manifest File

1

Add these to activities which you use nfc:

    <activity
        android:name="com.example.example.CLASSNAME"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">

        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>  
            <data android:mimeType="mime/type" />
        </intent-filter>

        <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
            android:resource="@xml/nfc_type"/>   

    </activity>

And also you have to define permissions before application tag.

2
  • There were a few comments, Opiatefuchs was first in the comments, followed by Burak. Since I can only upvote comments and accept answers. I am accepting this one. The other were all correct though.
    – Gerrie
    Commented Sep 15, 2014 at 12:04
  • @Gerrie I didn't understand your clarification about this accept?? Can you be clear?? Commented Sep 15, 2014 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.