【问题标题】:Suddently, "R.java" was deleted from my "CheckBox" program?突然,“R.java”从我的“CheckBox”程序中删除了?
【发布时间】:2014-03-07 13:27:35
【问题描述】:

我正在学习 Android(我是初学者)并为CheckBox 编写程序。
一切似乎都很好,但是当我突然清理我的项目时,R.java 被删除了,它给出了一个错误。
我检查了三遍,但没有恢复。

这是我的源代码:

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();
            }
        });
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 哎呀抱歉,我编辑了我的按钮标签仍然没有收到!

标签: android checkbox r.java-file


【解决方案1】:

您的 Button Tag 未关闭到 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 类。

【讨论】:

  • 哎呀对不起,我编辑了我的按钮标签仍然没有得到!
  • @developerknownasInsane 你清理了项目吗?转到错误窗口,查看 xml 中是否存在任何错误。
  • @developerknownasInsane 确保你的项目的XML文件没有错误,一一检查。
  • 顺便说一句,Eclipse 是个大问题,你更喜欢用什么 IDE 来开发 Android?
  • :) 我曾经使用过的 Android Studio。但是eclipse也是一个不错的IDE。有时这不会清除项目(基本上是重建项目),所以你需要重新启动 eclipse。顺便说一句,你可以使用 Android Studio。
【解决方案2】:

将您的按钮更改为以下内容:

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

您似乎忘记关闭按钮标签,因此 AAPT 无法生成新的 R.class。

【讨论】:

    【解决方案3】:

    当您在一个或多个 xml 文件上出现错误时,通常不会创建 R.java。在这种情况下,错误出现在 Button 标记中(IDE 应该警告您该错误)。

    【讨论】:

    • 哎呀抱歉,我编辑了我的按钮标签仍然没有收到!
    • 您使用的是什么 IDE?是日食吗?你能发布你的xml文件吗?
    • 该文件似乎是正确的,尝试执行清理
    • 我的问题已解决 ..thanx 顺便回复 .. +1 由我 ;) 回复!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    相关资源
    最近更新 更多