Home > Software engineering >  Why getting errors in android studio when adding code to the main activity xml?
Why getting errors in android studio when adding code to the main activity xml?

Time:02-16

I'm using android studio 2022 language is java

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

        imageButton = findViewById(R.id.button);
        editText = findViewById(R.id.edittext);

        if(ContextCompat.checkSelfPermission( this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(activity: this, string[]{Manifest.permission.RECORD_AUDIO} , requestCode.1);
        }
    }

Getting errors in the line

ActivityCompat.requestPermissions(activity: this, string[]{Manifest.permission.RECORD_AUDIO} , requestCode.1);

The activity is not exist it should be much smaller and in gray. requestCode also is not exist.

screenshot image of the errors :

Errors

CodePudding user response:

You may need to take a course on Java in general before you start jumping into Android which far more complex. Every argument for requestPermissions is not valid Java

activity: this is not valid Java. Just use this.

string[]{Manifest.permission.RECORD_AUDIO} is not valid Java. Change it to this new String[]{Manifest.permission.RECORD_AUDIO}

requestCode.1 is not valid Java just use 1

The end result should be this

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 1);
  • Related