Home > Enterprise >  Multiple Permissions Android
Multiple Permissions Android

Time:07-23

I want to build java class that managing all my permissions I start with ReadContacts from Phone Permissions.java

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class Permissions {


    public void checkPermission(Context t, Activity s) {
        //check condition
  
        if(ContextCompat.checkSelfPermission(t, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED){
            //when permission is not granted
            //Request permission
            ActivityCompat.requestPermissions(s,new String[]{Manifest.permission.READ_CONTACTS},100);

        }else{
            //when permission is granted
            //create method

        }
    }



}

and I pass from MainActivity the Context and activity

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public Permissions permissions;
    Context appContext;
    Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appContext = getApplicationContext();


        permissions.checkPermission(appContext,MainActivity.this);
    }


    private void checkPermission() {
        //check condition

        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED){
            //when permission is not granted
            //Request permission
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_CONTACTS},100);

        }else{
            //when permission is granted
            //create method
            Toast.makeText(this,"permission Access",Toast.LENGTH_SHORT);
        }
    }

}

and what it's does is crashing my app I know that my problem it when I pass the context and the activity , I try to only send the activity and its also crashing my app

Thanks for Assistance

CodePudding user response:

It seems that you have not instantiated the permissions of type Permissions in your MainActivity class. Thus you're calling the checkPermission() method on a null object reference.

Possible Fixes

public class MainActivity extends AppCompatActivity {
    public Permissions permissions;
    Context appContext;
    Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appContext = getApplicationContext();

        permissions = new Permissions();
        permissions.checkPermission(appContext,MainActivity.this);
    }
    
    // Rest of the code...
}

Alternatively...

You can alternatively declare the checkPermission method as static to be able to call it without instantiating.

public class Permissions {

    public static void checkPermission(Context t, Activity s) {
        //check condition
  
        if(ContextCompat.checkSelfPermission(t, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED){
            //when permission is not granted
            //Request permission
            ActivityCompat.requestPermissions(s,new String[]{Manifest.permission.READ_CONTACTS},100);

        }else{
            //when permission is granted
            //create method

        }
    }
}

then you call it in the MainActivity like following

public class MainActivity extends AppCompatActivity {
    
    Context appContext;
    Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appContext = getApplicationContext();

        Permissions.checkPermission(appContext,MainActivity.this);
    }
    
    // Rest of the code...
}
  • Related