Android - setContentView(R.layout.main);
Change
setContentView(R.layout.main);
to
setContentView(R.layout.activity_main);
as you dont have an xml file named main.xml in your layout folder..
what does setContentView do?
There's one thing that must be clarified here.
It's true that setContentView(R.layout.acivity_main2);
will link the layout file to your Activity. However, tools:context=".Main2Activity"
does not link your Activity to your layout file in the way you're thinking it does.
First, you need to take note of the word tools
in that line. tools
is the name typically used for the tools
namespace which provides a few xml attributes that only works during design-time. Take a look at this documentation: https://developer.android.com/studio/write/tool-attributes
Note how it said that when you're building your app, everything related to tools
will be stripped out. Therefore, tools:context=".Main2Activity"
plays zero role in linking your Activity to the layout.
Instead, it's only meant to link it during design, by allowing you to Preview your layout in a container that mimics the Activity you set in tools:context=
. So if you have a certain Theme for that Activity, the Preview will reflect the theme in the Preview Window.
On the other hand, setContentView(R.layout.activity_main2);
does play a major role in linking your xml layout to your Activity.
That statement is telling the Activity to inflate that xml layout and fetch all of it's Views and more. Essentially, by using setContentView(R.layout.activity_main2);
you're telling the Activity
that you want to use that layout file, so please create your layout using that file as the guidelines.
After you called setContentView(R.layout.activity_main2);
your Activity will know which layout you want to use, therefore making it possible to find the Views you want to work with through methods like findViewById()
. This wouldn't be possible if you never used setContentView()
.
Therefore, to answer your last two questions:
Switching Main2Activity to another Activity in your layout xml file will do nothing besides show you how your layout will look if it was placed inside the other Activity. It won't change the outcome of your app at all. All for designing purposes.
Switching to another layout file in
setContentView()
will completely change the layout used in the Activity. Be careful of doing this, since if you have code used only for Views in the original layout, this WILL cause a crash if that code runs for the 2nd layout, because those Views don't exist anymore.
Bottomline is:
tools:context=".Main2Activity"
- This is unnecessary for connecting your Activity and layout.setContentView(R.layout.acivity_main2);
- This is necessary for connecting your Activity and layout.
Error: setContentView(R.layout.main) in Android Studio
I found the answer by my self. I thing the error is not because of wrong import as answered here. It was because of the use of wrong name
and id
of layout file and check box
respectively. My .xml
layout file name was activity_my_checkbox.xml
. So I should use:
setContentView(R.layout.activity_my_checkbox);
And my check box id was c
instead of check
, so I should use
cb=(CheckBox)findViewById(R.id.c);
setContentView(R.layout.main); Gives error from start
this is quite a common mistake .
make sure that at the top of your java file of the activity , the import is not of android.R.layout (or whatever) ,since it should import the R of your own app (which is inside the gen folder).
if you still have problems with that , try to delete the gen folder , make sure the layout file is ok (meaning it's a correct android layout file, without any mistakes), make sure all of the resources (all images, sounds , xml files , all that are in /res) have only the legal characters in their files names (which means they are lowercase english ,optionally with "_" and digits, but no more) , clean the project , and then import the generated file .
you can also use CTRL+1 on the red underline of the R that it complains about , and choose the one of your project.
setContentView(R.layout.Main); Main cannot be resolved or is not a field
First just build your project. If it will not work, then delete from imports android.R.layout and use layout from your package.
setContentView(R.layout.main); error
Just take 2 steps and problem would be more likely to get solved:
Step 1:
Clean your project by clicking Project -> Clean.
Step 2:
Rebuild your project by clicking Project -> Build All.
Also make sure that your layout xml files are syntax error free and you don't have any image which has non-acceptable names (such as a "-" between image name).
Also I request you to have a look at problems window and let me know what errors are being shown there.
Related Topics
Android Manifest Restrict to Tablets
How to Change the Font Size of Tabhost in Android
Android: Failed to Allocate Memory
Calling Hidden API in Android to Turn Screen Off
How to Default to Numeric Keyboard on Edittext Without Forcing Numeric Input
Jpeg Images Have Different Pixel Values Across Multiple Devices
Avd Manager - Cannot Create Android Virtual Device
How to Control the Scrolling Speed of Recyclerview.Smoothscrolltoposition(Position)
Android: Application-Wide Font-Size Preference
Android Alarmmanager Not Waking Phone Up
List View Items Changes Position When Scrolling Android
Adb Got Two Same Serial Numbers When Connected to Two Smart Phones
How to Disable Status Bar Click and Pull Down in Android
How to Highlight Row in Listview in Android
Error:Connectionresult{Statuscode=Internal_Error, Resolution=Null}
What Do I Do About: This Requires Android.Permission.Interact_Across_Users_Full