Setcontentview(R.Layout.Main); Gives Error from Start

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); 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.

Android Studio: setContentView(R.layout.activity_main); error:cannot resolve R

I dont know what the problem was, I tried a few of the solutions provided here: Execution failed app:processDebugResources Android Studio

None of them worked though. i just reinstalled Android Studio and did not update any of the build tools and its working fine now.

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..



Related Topics



Leave a reply



Submit