Android Studio - setContentView Wont Display New XML Layout

551 Views Asked by At

Very New to Android Studio and I am following a simple Udemy tutorial. I have:

  1. Successfully created a main.xml file
  2. Successfully utilized the file in the code: setContentView(R.layout.main);

Per the tutorial, I created a second xml file, with just a simple button, named "test.xml". I followed the instructions to utilize it in the setContentView:

setContentView(R.layout.test);

However, every time I run the emulator after the change nothing happens - I get a white screen that quickly closes, the app does not open. In the Run section I get the message "Success Operation succeeded." The build section states "finished."

If I repeatedly replay the app I get a message in the emulator that states: "App name keeps stopping" but no real info beyond that.

I think I'm creating the new xml file correctly: In the res folder -> File New -> Android Resource File -> Give it a file name (e.g. test). The new xml file then falls into the layout folder in my res folder.

I've created a number of xml files and I always get the same result. I don't see anywhere showing me an error.

I'm at lost for what I'm doing wrong in such a simple exercise.

Below is my onCreate code:

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

Again - if I change "testa" to "main" it works.

Attached is the xml code for main and test files. xml file 1 xml file 2

1

There are 1 best solutions below

1
Nikit Dungrani On

For displaying test.xml u need to add this to your xml file

      tools:context=".MainActivity"

and your .java file must named with MainActivity.java .

inshort if you want to bind any xml file with java class then add above attribute to main layout.and java file name must be same as in the tool:context="._____"