简体   繁体   中英

Eclipse - “main cannot be resolved or is not a field” (Android App programming)

I've got a problem with Eclipse. I use the ADT to develop Android apps directly in Eclipse. But right after creating a new project, I got an error in my "MainActivity.java"-file.

I googled and everywhere its said that I have to import an ".R" class from my project folder, which didnt work for me. I also completetely redownloaded Eclipse+ADT in one package this morning.

The line which gives the error is the following:

setContentView(R.layout.main);

Without the "import com.example.myapp.R" statement I've got the same error. Cleaning and rebuilding also didnt have any effect.

Do you have any ideas? Thanks!

Try to type R r; in your code. Hover your mouse over the 'R' and Eclipse will say " R cannot be resolved to a type ". Click import R (com.example.myapp) . If this doesn't work then check for the folder gen to say Generated Java Files. Also check to see if they have the classes inside.

R.layout.main refers to a layout XML file in your "res/layout" directory. This seems like the layout file you are trying to reference is named something else. . You just need to make sure that your referencing the correct layout in your onCreate method.

setContentView(R.layout.activity_main);

Post the main.xml. Make sure you import your application's R file and not android.R. Restart eclipse, do a clean up by going to your menu Project-> Clean , refresh and tell us if it is fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM