-1

I updated several of my files but they do not appear as id #s in R.java. I tried updating my imports, I tried closing then opening Eclipse, saving, and closing/opening the Emulator, but it is not working to get R.Java to recognize the updates in the files. Of the items that are missing from R.java are 2 buttons (with 2 services) which would activate a toast when pressed, I will list the files below, each one. I run the emulator (Virtual, Nexus API 17) and it shows an old version of my app that has no buttons, only text that says "Hello World". Thanks in advance.

UPDATE: I tried to clean my project (thinking that if the R.java got regenerated, then it might update itself), but it just made the R.java disappear, and created an error in the main folder, HelloWorld in the Package Explorer. Oddly enough, there were no other errors in the entire series of files that I edited. No yellow warnings, nothing. Plus I never edited the R.java file (I know it's a no-no). So then after I tried to run my project anyway, this error posted in the LogCat below.

I found this in the LogCat:

01-03 06:54:13.977: E/ThrottleService(300): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)

Here are my files:

MainActivity.java

package com.example.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

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

       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
       }

       // Method to start the service
       public void startService(View view) {
          startService(new Intent(getBaseContext(), MyService.class));
       }

       // Method to stop the service
       public void stopService(View view) {
          stopService(new Intent(getBaseContext(), MyService.class));
       }

}

MyService.java

package com.example.helloworld;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
   @Override
   public IBinder onBind(Intent arg0) {
      return null;
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      // Let it continue running until it is stopped.
      Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      return START_STICKY;
   }
   @Override
   public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
   }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <Button android:id="@+id/btnStartService"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/start_service"
   android:onClick="startService"/>

   <Button android:id="@+id/btnStopService"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/stop_service"
   android:onClick="stopService" />

</LinearLayout>

strings.xml

<resources>

    <string name="app_name">HelloWorld</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="start_service">Start Service</string>
    <string name="stop_service">Stop Service</string>

</resources>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

  <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity
           android:name=".MainActivity"
           android:label="@string/title_activity_main" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
       <service android:name=".MyService" />
   </application>
</manifest>

and finally, the R.java (which ignores many strings and other names)

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.helloworld;

public final class R {
    public static final class attr {
    }
    public static final class dimen {
        /**  Default screen margins, per the Android Design guidelines. 

         Customize dimensions originally defined in res/values/dimens.xml (such as
         screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.

         */
        public static final int activity_horizontal_margin=0x7f040000;
        public static final int activity_vertical_margin=0x7f040001;
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int action_settings=0x7f080000;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
    }
    public static final class menu {
        public static final int main=0x7f070000;
    }
    public static final class string {
        public static final int action_settings=0x7f050001;
        public static final int app_name=0x7f050000;
        public static final int hello_world=0x7f050002;
    }
    public static final class style {
        /** 
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.


        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.

 API 11 theme customizations can go here. 

        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.

 API 14 theme customizations can go here. 
         */
        public static final int AppBaseTheme=0x7f060000;
        /**  Application theme. 
 All customizations that are NOT specific to a particular API-level can go here. 
         */
        public static final int AppTheme=0x7f060001;
    }
}
Azurespot
  • 3,066
  • 3
  • 45
  • 73
  • 1
    Are there any errors in res folder in your project ? If there are any errors in res, R.java never generate again. – Hardik Joshi Jan 03 '14 at 07:07
  • @Noni Azure Just uninstall your application from the emulator and try to run the application again. – GrIsHu Jan 03 '14 at 07:14
  • Prince, no there are no errors in my res/ folder @shiju B, i tried to clean it, but then got errors since R.java did not regenerate. then i //commented out all the R.java calls, then cleaned again, then a red X box shows up on my HelloWorld folder in my package explorer (and R.java did not regenerate a 2nd time), but i am looking everywhere and i see no errors anywhere in other files, not even yellow warnings. strange. – Azurespot Jan 03 '14 at 07:20
  • GrlsHu, sorry i am still a beginner, how do i uninstall the emulator? – Azurespot Jan 03 '14 at 07:22
  • Never manually update R.java. – Hardik Joshi Jan 03 '14 at 07:22
  • @NoniAzure Have a look : http://stackoverflow.com/a/2757131/1405983 – Hardik Joshi Jan 03 '14 at 07:24
  • Checkout my updated answer to uninstall the application. @NoniAzure – GrIsHu Jan 03 '14 at 07:25
  • @GrIsHu buddy user facing error in eclipse project. After uninstalling application from emulator errors resolved ? – Hardik Joshi Jan 03 '14 at 07:26
  • @Prince Check out user's updated answer. He is facing another issue. – GrIsHu Jan 03 '14 at 07:27
  • @NoniAzure Have you added any library into your project ? – GrIsHu Jan 03 '14 at 07:27
  • GrlsHu, i have not added a library that i know of, but just copied code from a book. they were trying to teach us how to use Services. – Azurespot Jan 03 '14 at 07:31
  • Okey! so using uninstalling application from emulator, R.java generate again ? – Hardik Joshi Jan 03 '14 at 07:31
  • here is the original tutorial (just one page) and i copied what they suggested in the files they suggested. http://www.tutorialspoint.com/android/android_services.htm – Azurespot Jan 03 '14 at 07:33
  • @GrIsHu I already read entire question from first and after edited. You answer still not seems right. User have issue in building project/ she menually updated R.java / Forgot add library projects in build path. I think TalhaQ is right. I also checked by creating eclipse project. There are no errors in my project. – Hardik Joshi Jan 03 '14 at 07:36
  • @Prince, is there a way to find if i have an incompatible library? i checked the Android Private Libraries in my package explorer, but there are no red errors over any of them. technically though, i do not recall installing any libraries. – Azurespot Jan 03 '14 at 07:37
  • @NoniAzure Did you check by downloading project which TalhaQ given in answer ? – Hardik Joshi Jan 03 '14 at 07:42
  • @Prince, yes his file worked! but i must find out why before i check his option as the answer, because the answer will not be useful for anyone else, unless i can explain why it worked. a very strange bug though, i don't know why it was saying there was an error, when there were no visible errors in any of the files. – Azurespot Jan 03 '14 at 07:55

3 Answers3

3

You code is perfectly fine.I think you might have damaged R.java and put something by your self.I have compiled your code it works fine.You can even download from this link.

Talha Q
  • 4,350
  • 4
  • 28
  • 40
  • TalhaQ, i imported your SampleService folder into Eclipse, then run it, and wow that worked! how did you do that? create a new project, then copy/paste the code from all those files, then save it so that the R.java would update? I am very curious how this got to work now! Thanks so much! – Azurespot Jan 03 '14 at 07:48
  • Always Welcome.Yes i just copy and paste your code in new project and run it.FYI never mess with the R.java. :) – Talha Q Jan 03 '14 at 07:51
  • Oh TalhaQ, now I tried to do this, but it is doing the same thing as before. i tried to copy and paste the MainActivity.java, the strings.xml, the MyService.java, and AndroidManifest.xml, but it is giving the same error over the HelloWorld folder. Also, i looked into your MyService file, and a `Log.d("Service Started","Service Started");` and 'Log.d("Service Destroyed","Service Destroyed");' was added. did you manually do this? did you manually change anything in the files? because that is very important to include. – Azurespot Jan 03 '14 at 08:16
  • thanks again TalhaQ, your answer was not 100% correct, so i compiled everything into one answer, with a detailed explanation of what worked in the end. but yours was very helpful, much appreciation! – Azurespot Jan 03 '14 at 09:01
  • Yes i added them to ensure that service started/destroyed.Are you replacing the code in your current project??I advice you to create a new project and do all the same things as you mentioned above :) – Talha Q Jan 03 '14 at 09:02
  • how my answer wasn't correct? are you find any errors in my code? :P – Talha Q Jan 03 '14 at 09:06
  • Oh no, it was correct TalhaQ! :D but i did not alter my R.java, so I didn't want people to think that part of what you said was true. plus i made a new discovery that the order of copy/pasting (or just creating the files) was important, so i thought i should combine into a more full answer. – Azurespot Jan 03 '14 at 21:29
  • Also TalhaQ, i did copy the code as it was originally, so i left out any additions you made, and it worked. it really must have been the order of operations that prevented R.java from updating properly. – Azurespot Jan 03 '14 at 21:33
3

After great help from everyone, the final solution was to begin a new project, then copy/paste each important file back into it, including updating the AndroidManifest.xml file. However, the order of operations was very important. The rebuild did not work if I copy and pasted the MainActivity.java file first, rebuilding that way gave me that odd HelloWorld folder error with no other errors in all my other files. So I thought maybe rebuilding in a certain order would work, and it did.

I had to do the rebuild in this order:

  • strings.xml
  • activity_main.xml
  • MyService.java
  • update the AndroidManifest.xml (with the addition of the MyService.java in <service> tags)
  • then finally the MainActivity.java

I then updated all imports and saved everything, and low and behold, R.java was updated with the right ids for all of my strings and other name ids.

So while I still don't understand why I had a red error on my HelloWorld project folder in Package Explorer (that did not show a single error or warning anywhere else in any of my edited files), it looks like rebuilding the file by cutting and pasting in a new project, in a careful order, ended up updating the R.java so that I could run successfully in the emulator.

Special thanks to @TalhaQ for suggesting my files were okay and to reuse the code. Although I know I did not edit the R.java file at all, it's still a mystery why it never updated like it should have. Might have had something to do with the order of operations in the original file, since the order of operations mattered a lot in the rebuild.

Azurespot
  • 3,066
  • 3
  • 45
  • 73
-1

As you have stated in your question it shows an old version of my app that has no buttons, only text that says "Hello World".

Just try to uninstall the old application from your emulator and try to run the project.

Go to Settings>ApplicationManager> select your application> Uninstall

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • This is reason why R.java not generated ? – Hardik Joshi Jan 03 '14 at 07:18
  • @Prince Just wait before i update my answer. I know its not reason. – GrIsHu Jan 03 '14 at 07:18
  • thanks GrlsHu, i don't see Settings in my ADT menu, I'm using Android Developer Tools Build: v22.3.0-887826 I could go to the Virtual Device Manager to delete the emulator, should i try that? – Azurespot Jan 03 '14 at 07:28
  • Check out in your emulator the Settings icon will be there. – GrIsHu Jan 03 '14 at 07:29
  • thanks GrlsHu, i checked the icons in the Emulator, i found Dev Settings only, and did not see an uninstall option. i also looked in the upper file menu, but that is very limited, only has an About, a Quit, and a Minimize (Window). – Azurespot Jan 03 '14 at 07:53
  • thanks for your suggestion GrlsHu, what happened was the R.java was not updating, then i tried to clean it, then R.java disappeared and showed an odd error, and I think that you came in at the beginning before my R.java disappeared, so it evolved into an R.java issue at a point in the process. i will consider this answer another time though! – Azurespot Jan 03 '14 at 09:04