Apparently, "R.java" has been removed from my CheckBox program?

I am learning Android (I am new) and am making a program for CheckBox.
Everything seems fine, but when I cleaned up my project all of a sudden, R.java was deleted and it gave an error.
I checked three times, but did not get 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.

0
source share
3 answers

Button tag is not closed in activity_main.xml

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

Close that

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

and clean up the project. Your class R.javawill be generated.

+4
source

Change your button to the following:

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

It seems you forgot to close the button tag, so AAPT was unable to create a new R.class.

+1

Typically, R.java is not created when you have errors in one or more xml files. In this case, the error is indicated in the Button tag (the IDE should warn you about this error).

+1
source

All Articles