1
votes

I'm using following code of contact fetching app i'm getting Android runtime : fatal exception : main error at line no 17.

package com.example.alpesh_pc.contactlist;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.Toast;

public class MainActivity extends Activity
{
    public Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cursor=this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if(cursor.getCount()>0)
        {
            Toast.makeText(this, "Seccess", Toast.LENGTH_LONG).show();
        }

    }
}

below are my logcat

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.alpesh_pc.contactlist, PID: 3833 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alpesh_pc.contactlist/com.example.alpesh_pc.contactlist.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{6970ad0 3833:com.example.alpesh_pc.contactlist/u0a67} (pid=3833, uid=10067) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{6970ad0 3833:com.example.alpesh_pc.contactlist/u0a67} (pid=3833, uid=10067) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS at android.os.Parcel.readException(Parcel.java:1683) at android.os.Parcel.readException(Parcel.java:1636) at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4169) at android.app.ActivityThread.acquireProvider(ActivityThread.java:5434) at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2267) at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1515) at android.content.ContentResolver.query(ContentResolver.java:514) at android.content.ContentResolver.query(ContentResolver.java:472) at com.example.alpesh_pc.contactlist.MainActivity.onCreate(MainActivity.java:17) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  Application terminated.

3
please share your logcat - Tahmid Rahman
please check the question again i have modified it with logcat. thank you. @TahmidRahman - alpeshsarvaiya codizious
please check my ans - Tahmid Rahman

3 Answers

1
votes

Add permission <uses-permission android:name="android.permission.READ_CONTACTS" /> in Android manifest xml

0
votes

check permission in Manifest.xml as well as put Runtime permission for api level 23 or more like:

for Manifest file:

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

and for Runtime permission check:

permission_tutorial

0
votes

I think you are using Marshmallow OS (Android Version 6) or any device that needs runtime permission check.

Did you try this?

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {

    // Show an explanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

}else{

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
    }
}

And handle the permission request response

@Override
public void onRequestPermissionsResult(int requestCode,
       String permissions[], int[] grantResults) {
       switch (requestCode) {
          case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
           // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the
            // contacts-related task you need to do.

        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request
    }
}

Still if any confusion remains, you might have a look here.