2013-01-07 79 views
5

作为Android应用程序的开发人员,我是新来的。我发现一个问题。那是R.layout.main无法解析。我该如何解决我的问题。我的代码在这里。请解决我的问题。如何解决我的android应用程序无法解析R.layout.main?

package com.android; 

import android.R; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class HelloActivity extends Activity { 
    /** Called when the activity is first created. */ 

    Button bt; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     bt=(Button)findViewById(R.id.ButtonOk); 
     bt.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       //Toast.makeText(getBaseContext(), "Welcome Android World", 3000).show(); 
      Intent intent = new Intent(HelloActivity.this, DisplayActivity.class); 
      startActivity(intent); 
      } 
     }); 
    } 
} 




* 

和我的XML代码是在这里:

*

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ok" android:id="@+id/ButtonOk" android:height="50dp" android:width="100dp"></Button> 
</LinearLayout> 
+2

删除进口'android.R;'然后按'CTRL + SHIFT + O'用于导入您的应用程序R –

+0

,除了上面的注释之外,在eclipse中清理项目并再次构建。这个问题是一个无休止的重复 – quinestor

+0

这已被回答了一百次..请问谷歌之前你问 – krilovich

回答

0

尝试清理和重新构建应用程序,这应该解决的问题。

1

导入com.android.R不android.R因为你的包名是com.android

6

删除线进口android.R从标题。

,做一个按Ctrl + Shift + O

如果android.R再次出现,然后手动写

import <yourpackagename>.R 

看起来你作为com.android.R包名。理想情况下,避免使用com.android等软件包名称。尝试保持它像 com.companyname.appname

所以你进入这种习惯,并没有一旦你要发布改变所有文件夹中的包名称的痛苦。

7

步骤1:删除import android.R;

步骤2Clean And Rebuild(它应该工作)

如果没有

然后close the project退出Eclipse和再次打开。按照它应该工作的步骤。

如果没有

更改你的包

import android.R;

import yourpackage.R;

它应该工作

0

我的朋友, 检查你的XML文件的名称是“主”,因为我们总是写的setContentView(R.layout。your_xml_file_name); 如果你发现我的解决方案值得那么请喜欢

0

我也遇到了IntelliJ IDEA的这个问题,这是我能解决的问题,但首先我注意到以下几点:

  1. 这个问题并没有阻碍应用程序的执行
  2. 位于out/production/my/package /名称目录中的资源文件(R.java)被自动更新,但资源文件(R.java)位于gen/my.package.name目录未自动更新Ÿ
  3. IntelliJ IDEA的使用未更新的R.java的intellisence

所以,我复制的是在R.java文件生成出/生产/我/包布局类/名目录如下面

public static final class layout { 
    public static final int main = 2130903040; 

    public layout() { 
    } 
} 

和我在GEN/my.package.name目录

ñ粘贴到R.java文件这OTE: 请了,请不要修改自动生成的资源文件R.java文件出/生产/我/包/名称