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>
AXISHKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris Harte2015 Top Expert (Most Article Points)Commented:
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

0
AXISHKAuthor Commented:
Where should I add the code in my Java program ?

Thx
MainActivity.java
0
Chris Harte2015 Top Expert (Most Article Points)Commented:
The permissions check goes in onCreate() and the method onRequestPermissionsResult() goes in the same class.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

AXISHKAuthor Commented:
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
0
Chris Harte2015 Top Expert (Most Article Points)Commented:
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
0
AXISHKAuthor Commented:
Still encounter the error..  Thx
Permission.png
0
Chris Harte2015 Top Expert (Most Article Points)Commented:
Include the line
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)

if that does not work you are going to have to set your minimum sdk to 23
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Android

From novice to tech pro — start learning today.