i have a problem with Run time permissions, i tried to manage them for Marshmallow and above but my application still crashes on runtime (not when i restart app) this is the code of activity:
in the Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Here the activity code ( not all but necessary to understand i think)
public class HomeActivityNew extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener, SeekBar.OnSeekBarChangeListener {
// here all variables ...
private boolean permissionToRecordAccepted = false;
private boolean permissionToWriteAccepted = false;
private String[] permissions = {"android.permission.RECORD_AUDIO", "android.permission.WRITE_EXTERNAL_STORAGE"}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_new);
int requestCode = 200;
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(permissions, requestCode);
}
init();
myHandler = new Handler();
slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN);
SetupToolbar();
SetupPager();
SetupDrawer();
iv_playpause.setTag("pause");
MobileAds.initialize(getApplicationContext(), getResources().getString(R.string.admov_id));
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
/* SharedPreferences pref = getSharedPreferences("logincheck", Context.MODE_PRIVATE);
SharedPreferences.Editor edit1 = pref.edit();
edit1.putString("login","true");
edit1.apply();*/
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 200:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
permissionToRecordAccepted=true;
}
if(grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED){
permissionToWriteAccepted=true;
}
break;
}
if (!permissionToRecordAccepted) HomeActivityNew.super.finish();
if (!permissionToWriteAccepted) HomeActivityNew.super.finish();
}
and this the error :
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=200, result=0, data=null} to activity {com.mypackage.HomeActivityNew}: java.lang.
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
variable grantResults
empty!
app on runtime shows the dialog window for permission and crashes at same time but when i restart the app it waits for permissions