2014-02-10 20 views
0

我正在學習Android(我是初學者)併爲CheckBox製作程序。
一切似乎都很好,但是當我突然清理我的項目時,R.java被刪除了,並且出現錯誤。
我檢查了三次,但沒有收回。突然,「R.java」從我的「CheckBox」程序中刪除?

這裏是我的源代碼:

activity_main.xml中

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

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="IPhone" /> 

    <CheckBox 
     android:id="@+id/checkBox2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Android" 
     android:checked="true"/> 

    <CheckBox 
     android:id="@+id/checkBox3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Windows Mobile" 
     android:checked="false"/> 

    <Button 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Display" /> 

</LinearLayout> 

MainActivity.java

package com.example.lesson06; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     addListenerOnButton(); 
     addListenerOnChkbox(); 
    } 
    public void addListenerOnChkbox(){ 
     CheckBox chkbx=(CheckBox)findViewById(R.id.checkBox1); 
     chkbx.setOnClickListener(new OnClickListener(){ 

      public void onClick(View v){ 
       if(((CheckBox) v).isChecked()){ 
        Toast.makeText(MainActivity.this, "Bro..try Android :)", Toast.LENGTH_LONG); 
       } 
      } 
     }); 
    } 

    public void addListenerOnButton(){ 
     final CheckBox chkbx=(CheckBox)findViewById(R.id.checkBox1); 
     final CheckBox chkbx2=(CheckBox)findViewById(R.id.checkBox2); 
     final CheckBox chkbx3=(CheckBox)findViewById(R.id.checkBox3); 
     final Button btn=(Button)findViewById(R.id.btn); 

     btn.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       StringBuffer result=new StringBuffer(); 
       result.append("IPhone check: ").append(chkbx.isChecked()); 
       result.append("\nAndroid check: ").append(chkbx2.isChecked()); 
       result.append("\nWindows Mobile Check: ").append(chkbx3.isChecked()); 

       Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 
} 

任何幫助將不勝感激。

+0

哎呀對不起,我編輯我的按鈕標籤仍然沒有得到! –

回答

4

您的按鈕標籤未封閉成activity_main.xml

<Button 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Display" 

關閉該

<Button 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Display" /> 

和清潔工程。將生成您的R.java類。

+0

哎呀對不起,我編輯我的按鈕標籤仍然沒有得到! –

+0

@developerknownasInsane你清理了這個項目嗎?轉到錯誤窗口,看看是否有任何錯誤存在於xml中。 –

+0

是的,我檢查了每個地方.. –

1

更改按鈕,如下:

<Button 
    android:id="@+id/btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Display"/> 

看來你忘了關閉按鈕標籤,所以AAPT無法生成新的R.class。

1

當您在一個或多個xml文件上發生錯誤時,通常不會創建R.java。在這種情況下,錯誤出現在Button標籤中(IDE會警告你有關該錯誤)。

+0

哎呀抱歉,我編輯我的按鈕標籤仍然沒有得到! –

+0

你在用什麼IDE?是Eclipse嗎?你可以發佈你的XML文件? – Massimo

+0

我已經在這裏發佈了一個xml文件.. –