-1

My Android application crashes the first time launching and it asks me about the user call permission. When I launch my app again then it works normally. I have tried the following code.

public void loadContactList() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
    } else {.....}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) {
    if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            loadContactList();
        } else {
            Toast.makeText(this, "Until you grant the permission, we canot display the list", Toast.LENGTH_SHORT).show();
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jimit
  • 21
  • 7

3 Answers3

0

Add the following code

String[] appPermissions =
            {Manifest.permission.READ_CONTACTS,
                    Manifest.permission.WRITE_CONTACTS};
    int PERMISSIONS_REQUEST_CODE = 234;

Call this Method from your on create

checkAndRequestPermissions();

Add the following method to your Main Activity

public boolean checkAndRequestPermissions() {
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String perm : appPermissions) {
            if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(perm);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), PERMISSIONS_REQUEST_CODE);
            return false;
        }
        return true;
    }
Praveen
  • 93
  • 1
  • 7
  • Nope still for the first time of launching it is crashing and asking me user permission and then for the second time launch it works normally starting from the authentication page. – Jimit Jan 22 '19 at 06:50
  • can you please copy paste the error shown in the logcat – Praveen Jan 22 '19 at 07:05
  • Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:380) at android.widget.ListView.setAdapter(ListView.java:575) at com.example.jimitt.elderlycomboclient.MainActivity.loadListView(MainActivity.java:214) at com.example.jimitt.elderlycomboclient.MainActivity.onCreate(MainActivity.java:80) – Jimit Jan 22 '19 at 07:42
  • ArrayAdapter adapter = new ArrayAdapter(this, R.layout.lv_item, contacts) lv.setAdapter(adapter); – Jimit Jan 22 '19 at 07:44
  • You are trying to get the size of the list when it is null in line 80 .Check for null before calling it. – Praveen Jan 22 '19 at 09:47
0

Your code is right but you set the codes on wrong places lets try this it will definitely help you....

In Your Main Activity...

private int STORAGE_PERMISSION_CODE = 1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == 
  PackageManager.PERMISSION_GRANTED) {
        filter();
        Toast.makeText(this, ""+arrayList, Toast.LENGTH_SHORT).show();
    } else {
        Permission.requestStoragePermission(MainActivity.this,
                  STORAGE_PERMISSION_CODE);
    }

}//on create closed

Permission method.....

//---------------------------------RuntimePermission-----------------------------//
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
    permissions, @NonNull int[] grantResults) {
    if (requestCode == STORAGE_PERMISSION_CODE)  {
        if (grantResults.length > 0 && grantResults[0] == 
     PackageManager.PERMISSION_GRANTED) {
            filter();
        } else {
            Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();

Permission.requestStoragePermission(MainActivity.this,STORAGE_PERMISSION_CODE);
        }
    }
}

after that make java class for getting permission....

public class Permission {
public static void requestStoragePermission(final Activity activity, final int 
       STORAGE_PERMISSION_CODE) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        new AlertDialog.Builder(activity)
                .setTitle("Permission needed")
                .setMessage("This permission is needed because of this and that")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(activity,
                                new String[] 
        {Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create().show();
    } else {
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
   STORAGE_PERMISSION_CODE);
    }
}

}

The main thing you have to remember is to put your working method at the right place like my is filter();

It will work for you because it work for me when i stuck in the same situation

Vipul Chauhan
  • 189
  • 4
  • 19
0

I just solved an exception. I did two things. Firstly, I removed the initialization of Contacts by
Contacts = new ArrayList<ContactModel> from the loadContactList() and added it into the onCreate() and Secondly I just changed the return datatype of loadContactList() from void to list. Then I returned the contacts in loadContacts() so that loadListView() can fetch this list and display it into the list view.

Community
  • 1
  • 1
Jimit
  • 21
  • 7