Link to home
Start Free TrialLog in
Avatar of AXISHK
AXISHK

asked on

Android Fatal Exception

Receive the error below when running an Android application :

03-02 17:12:19.893 3907-3907/com.example.user.sqliteprovider E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.user.sqliteprovider, PID: 3907
                                                                               java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{14c2b79 3907:com.example.user.sqliteprovider/u0a93} (pid=3907, uid=10093) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS

However, those permissions have been put inside AndroidManifest.xml. Any idea ?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.sqliteprovider">

    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="24" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Putting the permissions in the manifest is no longer enough. Since version 21 you now have to ask the user on install for explicit permission, and that means using checkSelfPermission(). There is a bit of explanation in this article
https://www.experts-exchange.com/articles/27099/The-new-Android-LocationApi-and-permissions.html
but that is a work around. To ask properly you also need to use onRequestPermissionsResult() which is a callback from checkpermissions.

Here is the guide link
https://developer.android.com/training/permissions/requesting.html

Here is some example code that probably will not help you at all but is taken from one of my apps available at github
https://github.com/Raging-Developer/Personal-Weather

private final int REQ_CODE = 111;

//Here be your permissions request, everybody else gets suppressed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
    int cluckup1 = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);

    if (cluckup1 != PackageManager.PERMISSION_GRANTED)
    {
        requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE}, REQ_CODE);
    }
}

@Override public void onRequestPermissionsResult(int req_code, String[] perms, int[] grants)
{
    switch (req_code)
    {
        case REQ_CODE:
            if(grants[0] != PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(this, "Location access denied", Toast.LENGTH_LONG).show();
            }
        break;
        default:
            super.onRequestPermissionsResult(req_code, perms, grants);
    }
}

Open in new window

Avatar of AXISHK
AXISHK

ASKER

Where should I add the code in my Java program ?

Thx
MainActivity.java
The permissions check goes in onCreate() and the method onRequestPermissionsResult() goes in the same class.
Avatar of AXISHK

ASKER

Thx. How about the following code ? Do I need in my coding ?

"if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)"

Already add the onRequestPermissionResult() but I still receive the following errors:

03-07 09:39:02.241 6231-6231/com.example.user.sqliteprovider E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.user.sqliteprovider, PID: 6231
                                                                               java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{10a65ef 6231:com.example.user.sqliteprovider/u0a84} (pid=6231, uid=10084) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                                                   at android.os.Parcel.readException(Parcel.java:1684)
                                                                                   at android.os.Parcel.readException(Parcel.java:1637)
                                                                                   at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4199)
                                                                                   at android.app.ActivityThread.acquireProvider(ActivityThread.java:5476)
                                                                                   at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2239)
                                                                                   at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1517)
                                                                                   at android.content.ContentResolver.query(ContentResolver.java:516)
                                                                                   at android.content.ContentResolver.query(ContentResolver.java:474)
                                                                                   at com.example.user.sqliteprovider.MainActivity.listContacts(MainActivity.java:86)
                                                                                   at com.example.user.sqliteprovider.MainActivity$3.onClick(MainActivity.java:49)
                                                                                   at android.view.View.performClick(View.java:5637)
                                                                                   at android.view.View$PerformClick.run(View.java:22429)
                                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
MainActivity.java
The
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
is so that it will only ask for permissions if the user is running Marshmallow or higher. It is not necessary.

The onRequestPermissionsResult() is a call back from checkSelfPermission() which you do not have in your onCreate(). Also my permissions are checking for location, you have to set yours to READ_CONTACTS  and WRITE_CONTACTS
Avatar of AXISHK

ASKER

Still encounter the error..  Thx
Permission.png
ASKER CERTIFIED SOLUTION
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial