0

I'm following the android tutorial and can't get hello world to work. I have the same problem as described in this this SO question and I tried to use this SO answer as a remedy. However it didn't work.

I still get activity_main cannot be resolved (line 12) and main cannot be resolved (line 19).

I simply started a new android project and my MainActivity.java looks like this (note that I use android.R. ...:

package com.arandomtesteinstein.testandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(android.R.layout.activity_main); /* <----- Error in line 12 */
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(android.R.menu.main, menu); /* <----- Error in line 19 */
        return true;
    }

}

What am I doing wrong?

EDIT:

OK, just to clarify, I'm following exactly (to my best knowledge) the official android tutorial. When I get to the point where I click 'run' eclipse throws the error mentioned above. I then found this SO answer (second link above) and tried to follow it, however it still didn't work. Strictly speaking, I am not developing or doing anything smart or dump, I'm just trying to follow the very initial steps of the tutorial

Community
  • 1
  • 1
pandita
  • 4,739
  • 6
  • 29
  • 51

4 Answers4

0

prefixing android. you are looking for a layout named activity_main in the android resources, that probably does not exists. Change

 setContentView(android.R.layout.activity_main);

with

 setContentView(R.layout.activity_main);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I think that is the default when you start a project. However this didn't work and hence I used the `android.` prefix as discussed in [this SO link](http://stackoverflow.com/a/11486315/2488942) linked above – pandita Nov 01 '13 at 11:23
  • it is not the default. Eclipse typically creates a default layout for you. You should use it. – Blackbelt Nov 01 '13 at 11:24
  • Ok, I just tried this and can confirm that it doesn't work. It gives me exactly the same error as discussed in the SO links I posted above. I used the eclipse default layout and immediately encountered this error without changing anything. Then I found what looked like a solution on SO (second link) but it still didn't work. – pandita Nov 01 '13 at 11:27
  • @pandita do you have R.java generated under gen folder? – Raghunandan Nov 01 '13 at 11:28
  • also check that the R class is imported came from your application – Blackbelt Nov 01 '13 at 11:30
  • @Raghunandan blackbelt how can I check this? Sorry this is the first time that I'm looking at eclipse or android – pandita Nov 01 '13 at 11:31
  • @Raghunandan ok, I have a `gen` folder but as far as I can see it is empty. How can I generate `R.java`? – pandita Nov 01 '13 at 11:34
  • @pandita you might have errors in your resources files like drawable layout strings.xml. check and fix them first. clean and build the project. `R.java` will not be generated if you have errors in resource files. – Raghunandan Nov 01 '13 at 11:35
  • @pandita do you have something inside res/layout? – Blackbelt Nov 01 '13 at 11:35
  • @blackbelt yes I have `activity_main.xml` untouched – pandita Nov 01 '13 at 11:36
  • @pandita fix the erros in your resource files clean and build import R from your package and you are good to go – Raghunandan Nov 01 '13 at 11:49
  • @Raghunandan how would I find these errors in the resource files? The problems shown by the eclipse debugger point me to line 13 and 19 but don't give much additional information – pandita Nov 01 '13 at 12:05
  • can you at least told us in which directory you are getting those errors? Eclipse has the Problems view (you can access it from the view menu) that can help you to understand where the problem is – Blackbelt Nov 01 '13 at 12:07
0

Yes, I have faced same problem as well and at that time I was facing that problem as there was no file in gen folder(R.java). Clean project to generate those files in that folder. and then run your project as mentioned in tutorial. It will might work for you.

akshay
  • 5,811
  • 5
  • 39
  • 58
  • I also don't have files in the gen folder, however cleaning the project doesn't seem to generate anything there either... Which files did you get there? – pandita Nov 01 '13 at 11:59
  • (R.java & BuildCongif.java) this files should be there in your gen folder-your package. Without this file your project will not run at all. So, Do one more thing, once you have cleaned your project, click on build project and if auto generated files are there after clean&build, then its ok. Otherwise there might be problem in your sdk, or eclipse. – akshay Nov 06 '13 at 04:43
  • it still doesn't work... I don't have any files in the gen folder. I'll reinstall eclipse and the sdk and see if it works then. Maybe I missed a dependency or something? Thanks anyway for your help – pandita Nov 09 '13 at 05:07
0

I had a similar problem once and it turned out that i had accidentally deleted

public static final class layout {
        public static final int activity_main=0x7f030000;
    }
    public static final class menu {
        public static final int main=0x7f070000;
    }

from the R.java file.

simon
  • 11
  • 1
0

Remove import android.R; from your activity's imports. And import R from your generated package.

Or use;

setContentView(<your_package_name_at_manifest>.R.layout.activity_main);

instead of

setContentView(android.R.layout.activity_main);

Edit: You must use your <your_package_name_at_manifest>.R class to reference your resources(By importing or directly referening). If this class is not visible under gen source folder, it means that you didn't build your project successfully. Posible reason for this is errors under res folder of your project.

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • I tried: `setContentView(com.arandomtesteinstein.testandroid.R.layout.activity_main);`. I removed `import android.R` but it still doesn't work :( – pandita Nov 01 '13 at 11:42
  • Then you should have error(s) at some of your resources under your res folder and your R for your application under gen package is not generated. Try to solve the errors at your resources first. then build your project, see your R class under gen package. – Devrim Nov 01 '13 at 11:46
  • Not yet... I'll try later on again. – pandita Nov 01 '13 at 23:57