Skip to content Skip to sidebar Skip to footer

Null Pointer Exception When Checking For Permission With Android.content.context.checkpermission

I need to check for permissions before querying the Android calendar for events. To do it, Android studio is warning that I need to follow a check before querying. The auto generat

Solution 1:

it's a separate class, controller: public class DummyData extends Activity { .... }

That is not going to work.

Never extend Activity unless it is a real activity, one that you will register in the manifest.

Never create an instance of an Activity via a constructor (e.g., the new DummyData() that you have somewhere in your code). Use startActivity() to display an activity that you have registered in the manifest.

As it stands, while your DummyData class may work from a compilation standpoint, it will not work at runtime. An Activity needs to be instantiated by the framework, and that is not the case with your DummyData.

Pass a realContext object to checkSelfPermission(), and pass a realActivity object to requestPermissions(). In this case, "real" means "handed to you from the framework".

Solution 2:

Use (Activity)mContext instead of this.

if(ContextCompat.checkSelfPermission((Activity)mContext,Manifest.permission.READ_CALENDAR)!=PackageManager.PERMISSION_GRANTED)    
        {
            ActivityCompat.requestPermissions((Activity) mContext, newString[]{Manifest.permission.READ_CALENDAR},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);
        }

Solution 3:

You have to write the right activity at the position "this" main problem in activity.

Try to write the code in MainActivity and test.

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this, newString[]{Manifest.permission.READ_CALENDAR},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}

Post a Comment for "Null Pointer Exception When Checking For Permission With Android.content.context.checkpermission"