在Android Activity中“R无法解析为变量”

时间:2013-12-19 15:17:48

标签: java android xml

我正在做小的Android应用程序,我添加了空白的xml文件,但它显示了一些错误,虽然我没有添加任何代码仍然显示错误,所以我清理项目,但现在我清洗后的活动,当我写

setContentView(R.layout.reminder_edit);

它给出的错误如R cannot be resolved to a variable, 对于另一个名为ReminderListaActivity的活动,我试过这个

setContentView(R.layout.reminder_edit)

error was for xml file not for "R "错误是:reminder_edit cannot be resolved or is not a field

xml文件在布局中,它显示为从未使用过的文件,所以如何解决这个问题,有人可以帮我解决这个问题

编辑代码已添加

reminder_edit.xml

<ScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

        <TextView
            android:text="@string/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />
        <EditText 

            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             android:text="@string/body"
             />
        <EditText 
            android:id="@+id/body"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:minLines="5"
            android:scrollbars="vertical"
           android:gravity="top" /> 

        <TextView 
           android:text="@string/date"

           android:layout_width="fill_parent"
           android:layout_height="wrap_content"

            />
        <Button 
            android:id="@+id/reminder_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Reminder_Time"
            />

        <TextView 
            android:text="@string/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            />
        <Button 
            android:id="@+id/remindertime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remindertime"
            />

        <Button 
            android:id="@+id/confirm"
            android:text="@string/confirm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            />

    </LinearLayout>
</ScrollView> 

java文件

public class ReminderEditActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder_edit);
    }

}

3 个答案:

答案 0 :(得分:4)

修复XML中的错误,以便资源编译器为您生成R.java

答案 1 :(得分:4)

从活动类

中的导入中删除导入android.R;

答案 2 :(得分:2)

要访问“R.layout.edit_reminder”xml文件,您必须导入“gen”文件夹下的“R”文件。此文件是自动生成的,因此您可以继续删除它,然后再次生成。现在,如果您有任何 xml错误,那么无法生成R文件,这表明您必须查看xml才能找到问题。

关于导入,您可以在活动类中找到它们,位于包声明后面的最右侧。

确保您导入的R是:

yourpackage.R;

而不是tahn

android.R;

有时快速编写代码 android.R 会导入,您甚至都不会注意到。这是我经常检查的第一件事,它已经发生在我身上好几次了。

接下来,我建议您查看控制台问题视图(如果未在您的视角中设置这些视图,请转到窗口&gt;显示视图&gt;其他并选择合适的)。

查找错误,xml文件中止构建。查找它正在讨论的xml文件,然后查找其中的错误。