Adam S Adam S - 3 months ago 7
Java Question

Error:(21, 8) error: class MyActivity is public - Can't edit MyActivity

When trying to edit MyActivity, it opens up

When trying to change the read only flag this error message shows up

Failed to change read-only flag for /root/AndroidStudioProjects/MyApplication3/app/src/main/java/xyz/adamstephenson/android/myapplication/

I'm trying to make a public class that outputs information when a button is pressed on Android.

The error is:

Error:(21, 8) error: class MyActivity is public, should be declared in
a file named


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.content.Intent;

import static;

public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {


final Button button = (Button) findViewById(;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("button", "This is my message");


This error occurs when your class file name is not same as the class name inside the class file.

in drive you make a class 
but write it as 
public class A1{}

It will throw exception : class A1 is public, should be declared in a file named

How ever if you do something like following
and code it as 
class A1{}

Above will not throw any exception, but this class will not be accessible from outside the package.