I'm learning Android (I'm a beginner) and making a program for CheckBox
.
Everything seems fine, but when I cleaned my project suddenly, R.java was deleted and it's giving an error.
I checked three times, but not getting it back.
Here is my source code:
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();
}
});
}
}
Any help would be greatly appreciated.
Your Button Tag not closed into
activity_main.xml
Close that
and clean project. Your
R.java
class will be generated.Generally R.java is not created when you have errors on one or more xml files. In this case the error is in the Button tag (the IDE should warn you about that error).
Change your button to the following:
It seems you forgot to close the button tag, so AAPT could not generate a new R.class.