Requesting runtime permission
Requesting runtime permission
I am trying to update this calendar agenda app to the latest SDK. Hence, I will need to request for runtime permission.
I have identify the class which need the READ_CALENDAR permission is CalendarUtilities.java, specifically this method:
public Set<org.reber.agenda.AndroidCalendar> getAvailableCalendars() {
ContentResolver contentResolver = context.getContentResolver();
Uri calendarsURI = CalendarContract.Calendars.CONTENT_URI;
Cursor cursor = contentResolver.query(calendarsURI,
new String{ CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_COLOR,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME },
null, null, null);
// Get all the enabled calendars in the database
while (cursor != null && cursor.moveToNext()) {
AndroidCalendar current = new AndroidCalendar(cursor.getString(0),
getColorHex(cursor.getString(1)), cursor.getString(2));
if (!calendars.contains(current)) {
calendars.add(current);
}
}
if (cursor != null) {
cursor.close();
}
return calendars;
}
I have tried:
public Set<org.reber.agenda.AndroidCalendar> getAvailableCalendars() {
askForPermission(Manifest.permission.READ_CALENDAR, 1);
return calendars;
}
private void askForPermission(String permission, Integer requestCode) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
ActivityCompat.requestPermissions(this, new String{permission}, requestCode);
} else {
ActivityCompat.requestPermissions(this, new String{permission}, requestCode);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions, int grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
ContentResolver contentResolver = context.getContentResolver();
Uri calendarsURI = CalendarContract.Calendars.CONTENT_URI;
Cursor cursor = contentResolver.query(calendarsURI,
new String{ CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_COLOR,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME },
null, null, null);
// Get all the enabled calendars in the database
while (cursor != null && cursor.moveToNext()) {
AndroidCalendar current = new AndroidCalendar(cursor.getString(0),
getColorHex(cursor.getString(1)), cursor.getString(2));
if (!calendars.contains(current)) {
calendars.add(current);
}
}
if (cursor != null) {
cursor.close();
}
//return calendars;
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
However, the app crash with the following crash log:
java.lang.RuntimeException: Unable to start activity ComponentInfo{/org.reber.agenda.AgendaActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
at android.content.ContextWrapper.checkPermission(ContextWrapper.java:669)
at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:439)
How to solve this?
Describe the error, does the app crash? Premission dialog doesn't show?
– Facundo
Jun 30 at 12:35
Please check my edited question. I have added the crash log.
– user2872856
Jun 30 at 12:37
Where are referencing your method getAvailableCalendars()?
– Facundo
Jun 30 at 12:46
1 Answer
1
You appear to be calling checkSelfPermission() from a field initializer or a constructor. Do not call checkSelfPermission() until onCreate() is called, preferably after the call to super.onCreate().
checkSelfPermission()
checkSelfPermission()
onCreate()
super.onCreate()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What does "it is not working" mean? What are your precise symptoms?
– CommonsWare
Jun 30 at 12:33