I want make a Util class to CheckPermission, but i'm getting some errors, i 
don't know what i'm doing wrong

public class CheckPermission extends Activity {
    private final Context context;
    private static final String MNC = "MNC";
    int permReq = 0;
    String permMan = "";
 
    public CheckPermission(Context context) {
        this.context = context;
    } 
 
    //perMan can be any code number higher than 0 
    public void requestPermission(String permRequested){
        switch (permRequested) {
            case "CAMERA": 
                //Request for Camera 
                this.permReq =  ContextCompat.checkSelfPermission(context, 
Manifest.permission.CAMERA);
                this.permMan = Manifest.permission.CAMERA;
                break; 
            case "INTERNET": 
                //Requesr for Internet 
                this.permReq =  ContextCompat.checkSelfPermission(context, 
Manifest.permission.INTERNET);
                this.permMan = Manifest.permission.INTERNET;
                break; 
            case "STORAGE": 
                //Request for group Storage - Read_External_Storage & 
Write_External_Storage 
                this.permReq =  ContextCompat.checkSelfPermission(context, 
Manifest.permission_group.STORAGE);
                this.permMan = Manifest.permission_group.STORAGE;
                break; 
            case "MICROPHONE": 
                //Request for group Microphone - Record_Audio 
                this.permReq =  ContextCompat.checkSelfPermission(context, 
Manifest.permission_group.MICROPHONE);
                this.permMan = Manifest.permission_group.MICROPHONE;
                break; 
            case "LOCATION": 
                //Request for group Location - Acess_Fine_Location & 
Acess_Coarse_Location 
                this.permReq =  ContextCompat.checkSelfPermission(context, 
Manifest.permission_group.LOCATION);
                this.permMan = Manifest.permission_group.LOCATION;
                break; 
            case "CALL": 
                //Requesr for call 
                this.permReq = ContextCompat.checkSelfPermission(context, 
Manifest.permission.CALL_PHONE);
                this.permMan = Manifest.permission.CALL_PHONE;
                break; 
            default: 
                break; 
        } 
    } 
 
    public boolean hasPermission( String permRequested){
        final PackageManager pm = context.getPackageManager();
 
        if(isMNC_Or_Higher()) { 
            requestPermission(permRequested);
            Toast.makeText(this.context, "Is MNC - permMan: " + this.permMan + 
" Perm required: " + permReq, Toast.LENGTH_SHORT).show();
 
            if (permReq != pm.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new 
String[]{this.permMan}, this.permReq);
                return false; 
            } 
        } 
        return true; 
    } 
 
    //check if is 6.0 or higher 
    public boolean isMNC_Or_Higher(){ 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return true; 
        } 
        return false; 
    } 
 
    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] 
permissions, int[] grantResults) {
 
        if (requestCode == this.permReq) {
            if (grantResults.length == 1 &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permissão concedida", 
Toast.LENGTH_SHORT).show();
            } else { 
                Toast.makeText(this, "Permissão negada", 
Toast.LENGTH_SHORT).show();
            } 
        } else { 
            super.onRequestPermissionsResult(requestCode, permissions, 
grantResults);
        } 
    } 
} 



ERROR:

PID: 25504 java.lang.NullPointerException: Attempt to invoke virtual method 
'android.content.Context android.content.Context.getApplicationContext()' 
on a null object reference at 
android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107) 
at 
com.dev.kamui.patrulhacomunitaria.CheckPermission.hasPermission(CheckPermission.java:68)
 
at 
com.dev.kamui.patrulhacomunitaria.Pagina_Principal$1.onClick(Pagina_Principal.java:47)
 
at android.view.View.performClick(View.java:5198) at 
android.view.View$PerformClick.run(View.java:21147) at 
android.os.Handler.handleCallback(Handler.java:739) at 
android.os.Handler.dispatchMessage(Handler.java:95) at 
android.os.Looper.loop(Looper.java:148) at 
android.app.ActivityThread.main(ActivityThread.java:5417) at 
java.lang.reflect.Method.invoke(Native Method) at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/2c617311-ffa1-4d22-ba93-98d4ba9b9bc5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to