2013-01-07 141 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文件出/生產/我/包/名稱

相關問題