21

Hi I am completely new to Android programming and the question I am asking might be something very simple, but I do not have any idea about how to make it work, so kindly bear with me.

I installed android-sdk and related tools from the Android developer site. I followed their instructions to create a HelloWorld app, but I am getting some errors which I don't understand.

The file MainActivity.java is an auto generated file and I keep getting errors in this file in these places:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main); // error in activity_main
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu); // error in activity_main
   return true;
 }

So I went and tried to look through the generated class file for layout.class and found there is no variable called activity_main there.

Can anyone kindly suggest what to do here? Any help will be deeply appreciated.

The directory structure shows me that I do have the AndroidManifest.xml.

When I try to run the app, I get this specific error:

     AndroidManifest.xml file missing!
     Unknown Android Packaging Problem

These are some of the errors that i am getting when trying to get it running :

        10-10 23:31:11.305: E/Trace(946): error opening trace file: No such file or directory (2)
        10-10 23:31:11.955: E/AndroidRuntime(946): FATAL EXCEPTION: main
        10-10 23:31:11.955: E/AndroidRuntime(946): android.content.res.Resources$NotFoundException: Resource ID #0x2
        10-10 23:31:11.955: E/AndroidRuntime(946):  at android.content.res.Resources.getValue(Resources.java:1013)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2098)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at android.content.res.Resources.getLayout(Resources.java:852)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at android.view.MenuInflater.inflate(MenuInflater.java:107)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at com.example.myfirstapp.MainActivity.onCreateOptionsMenu(MainActivity.java:18)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:393)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2913)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at android.os.Handler.handleCallback(Handler.java:615)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at android.os.Handler.dispatchMessage(Handler.java:92)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at android.os.Looper.loop(Looper.java:137)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at android.app.ActivityThread.main(ActivityThread.java:4745)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at java.lang.reflect.Method.invokeNative(Native Method)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at java.lang.reflect.Method.invoke(Method.java:511)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        10-10 23:31:11.955: E/AndroidRuntime(946):  at dalvik.system.NativeStart.main(Native Method)
The Dark Knight
  • 5,455
  • 11
  • 54
  • 95
  • 4
    You're probably missing AndroidManifest.xml :P – Raghav Sood Oct 10 '12 at 17:34
  • On a more helpful note, did you create an Android project or a normal Java project? – Raghav Sood Oct 10 '12 at 17:34
  • 1
    Right Click > DELETE . Create a new Android Application Project and see if runs with no errors. – baTimá Oct 10 '12 at 17:44
  • 1
    @sood : I have the xml and i have created an android project. – The Dark Knight Oct 10 '12 at 17:52
  • Ok guys thanks for your valuable inputs. Thanks to you all, i got it running. At first I(being a first class nitwit) did not build it. So when i saw errors i tried to resolve them using shift+O and it automatically got the package android.R. So after the suggestions when i built it, due to the package reference, i was getting more errors. So now i have removed the reference, built it and ran it. Everything's fine now. Thanks to all again for helping me out here at such a short notice. – The Dark Knight Oct 10 '12 at 18:22
  • 3
    Eclipse is retarted. I got the same error. Had to clean and build again, then it worked.... – Johan Jul 17 '13 at 08:10
  • I was having this same problem, even though my android manifest.xml was clearly there. What seemed to work for me was to close all open projects that were not needed. Go figure. :P !:^) – Sandi Laufenberg-Deku Oct 17 '15 at 22:21

6 Answers6

55

Just build your project or Clean it as "Clayton" mentioned below.

Project > Build All

Project > Clean

This I believe is because you do not have R.java file in your gen folder. Building your project will generate the R.java file in your gen folder and the errors should go away.

This happens because you're referring to your layout file using the following code

setContentView(R.layout.activity_main); 

The reference to R.layout.activity_main is declared in R.java file which needs to be generated by building your project.

Susie
  • 5,038
  • 10
  • 53
  • 74
  • 1
    I am a dummy. Forgot to build it. Now i have built it , the missing xml error is gone but the other two errors regarding the activity_main still are there. – The Dark Knight Oct 10 '12 at 17:54
  • 1
    Check your R.java file to see if the reference to 'R.layout.activity_main' is there. – Susie Oct 10 '12 at 17:58
  • Yes its there. I am adding the codes here : public static final class layout { public static final int activity_main=0x7f030000; } public static final class menu { public static final int activity_main=0x7f060000; } – The Dark Knight Oct 10 '12 at 18:08
  • 1
    Turn off automatically build and forget to build manually could cause this problem. – Jichao May 17 '14 at 05:44
38

Project --> Clean worked for me

Clayton Rabenda
  • 5,229
  • 2
  • 19
  • 16
2

- First of all please make sure that you have properly synced Eclipse with Android.

- Have you added the tools, and platform-tools folder Path in your System Environment Path.

- I hope you are creating an Android Project and not Java. See the Project folder in the Project explorer in Eclipse will be having a A sign for Android and J for Java.

keyser
  • 18,829
  • 16
  • 59
  • 101
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • Added the folders to path variable. Yes i have created android project. After a build , the missing xml error is gone but the two errors regarding activity_main are still there . – The Dark Knight Oct 10 '12 at 17:55
1

what worked for me:

  • Uncheck Project>Build Automatically
  • Close project
  • Open Project
  • Clean project
  • manually Project> Build project then it worked.

Eventually i checked back Build Automatically.

I do not know what`s with "Build Automatically", It also gives me some proble when i export my Google play games (especially when i used external libray, like AndEngine for instance).

To fix it, i do a similar thing to fix the problem (uncheck it, clean, build manually)

0

I had a similar problem. But is was caused by crashing of eclipse. After that I was 2 swap files of AndroidManifest.xml and the same error: "AndroidManifest.xml file missing!".

So, I removed swap files (.swo, .swp)

:-)

Meph-
  • 657
  • 1
  • 8
  • 20
0

This is build issue.

Go to Menu in eclipse, Project>clean then Project>Build All

Clean instruction will remove all build configuration and fix all build issues.

azesh
  • 135
  • 1
  • 3
  • 13