R.java没有显示按钮,字符串或服务

时间:2014-01-03 07:05:08

标签: java android eclipse r.java-file

我更新了我的几个文件但它们在 R.java 中没有显示为id #s。我尝试更新我的导入,我尝试关闭然后打开Eclipse,保存,关闭/打开模拟器,但它无法让 R.Java 识别文件中的更新。 R.java中缺少的项目是2个按钮(有2个服务),按下时会激活吐司,我将列出下面的文件,每个文件。我运行模拟器(Virtual,Nexus API 17),它显示了我的应用程序的旧版本,没有按钮,只有文本显示“Hello World”。提前谢谢。

更新:我试图清理我的项目(想想如果 R.java 重新生成,那么它可能会自行更新),但它只是让 R.java 消失,并在Package Explorer中的主文件夹HelloWorld中创建了一个错误。奇怪的是,我编辑的整个文件系列中没有其他错误。没有黄色警告,没有。另外,我从未编辑过R.java文件(我知道这是禁止的)。所以在我尝试运行我的项目之后,这个错误发布在下面的LogCat中。

我在LogCat中找到了这个:

  

01-03 06:54:13.977:E / ThrottleService(300):期间出现问题   onPollAlarm:java.lang.IllegalStateException:解析统计信息的问题:   java.io.FileNotFoundException:/ proc / net / xt_qtaguid / iface_stat_all:   打开失败:ENOENT(没有这样的文件或目录)

以下是我的文件:

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>

最后, R.java (忽略了许多字符串和其他名称)

/* 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;
    }
}

3 个答案:

答案 0 :(得分:3)

你的代码非常好。我想你可能已经损坏了R.java并自己放了一些东西。我编译了你的代码它工作得很好。你甚至可以从这个link下载。

答案 1 :(得分:3)

经过大家的大力帮助,最终解决方案是开始一个新项目,然后将每个重要文件复制/粘贴回其中,包括更新 AndroidManifest.xml 文件。但是,操作顺序非常重要。如果我首先复制并粘贴 MainActivity.java 文件,重建不起作用,重建那种方式给了我奇怪的HelloWorld文件夹错误,我的所有其他文件中没有其他错误。所以我认为按照某种顺序进行重建是可行的,而且确实如此。

我必须按此顺序进行重建:

  • 的strings.xml
  • activity_main.xml中
  • MyService.java
  • 更新 AndroidManifest.xml (在<service>代码中添加 MyService.java
  • 然后是 MainActivity.java

然后我更新了所有导入并保存了所有内容,并且低,并且, R.java 使用正确的ID更新了我的所有字符串和其他名称ID。

因此,虽然我仍然不明白为什么我在Package Explorer中的HelloWorld项目文件夹出现红色错误(在我编辑的任何文件中没有显示任何其他错误或警告),它看起来像通过在新项目中剪切和粘贴重建文件,仔细检查,最后更新 R.java ,以便我可以在模拟器中成功运行。

特别感谢@TalhaQ建议我的文件没问题并重用代码。虽然我知道我根本没有编辑 R.java 文件,但它仍然是一个谜,为什么它永远不会像它应该的那样更新。可能与原始文件中的操作顺序有关,因为在重建过程中操作顺序很重要。

答案 2 :(得分:-1)

正如您在问题it shows an old version of my app that has no buttons, only text that says "Hello World".

中所述

尝试从模拟器中卸载旧应用程序并尝试运行该项目。

转到设置&gt; ApplicationManager&gt;选择你的申请&gt;卸载