1

Hello I wanting to make a simple button click which allows the user to call a specific number when pressed. Before API 23 I code simply add the permission to the manifest and away we go but now I have to add this self check system. The problem I having with the line

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

As (this) has a red underline telling me it's the wrong 1st argument type. Being that this is my first time with this self check problem, how would I go about fixing the error?


call = (Button) findViewById(R.id.btnCall);

    call.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:123"));

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            startActivity(callIntent);




        }
    });

Thank you

james
  • 303
  • 1
  • 2
  • 14

1 Answers1

1

Eternal problem :)

Change this to getBaseContext() or to <YourActivityClass>.this

if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
    return;
 }
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • Thanks cleared the error :D sadly I get Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference -----when running the application now – james Apr 04 '16 at 19:40
  • @james looks like a `btnCall` doesn't exist in activity layout – Sergey Shustikov Apr 04 '16 at 20:06
  • Thanks sorted the problem – james Apr 04 '16 at 20:40