Java R.id 安卓工作室

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20729506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:50:24  来源:igfitidea点击:

R.id android studio

javaandroidandroid-studio

提问by keyboard lizard

I have a problem with android studio 0.4. There is and error with r.id. I think i messes something with the imports or activity_main.xml up because there is an error in main_xml, too. And when I hover over R.id, android studio whants me to write android.R.id.

我有 android studio 0.4 的问题。r.id 有错误。我想我在导入或 activity_main.xml 中搞砸了,因为 main_xml 中也有错误。当我将鼠标悬停在 R.id 上时,android studio 希望我编写 android.R.id。

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

lblTest = (TextView) findViewById(R.id.lblTest);

activity_main.xml (table marked red)

activity_main.xml(表标记为红色)

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnDice"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:id="table">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="false"
            android:id="@+id/btnFalse"
            android:layout_column="3"
            android:layout_below="table"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="true"
            android:id="@+id/btnTrue"
            android:layout_column="1"
            android:layout_below="table"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
</LinearLayout>

error messages:

错误信息:

String types not allowed (at 'layout_below' with value 'table').

不允许的字符串类型(在 'layout_below' 值为 'table')。

Cannot resolve method 'setContentView(?)'. Cannot resolve symbol 'R'.

无法解析方法“setContentView(?)”。无法解析符号“R”。

any ideas?

有任何想法吗?

采纳答案by Booger

It looks to me like you have an error in your XML. Sounds like you are trying to set a String attribute for a 'layout' parameter, when this should actually be a reference to another element in your layout.

在我看来,您的 XML 中有错误。听起来您正在尝试为“布局”参数设置 String 属性,而这实际上应该是对布局中另一个元素的引用。

Bottom line, check your layout.xml for a syntax error.

最重要的是,检查您的 layout.xml 是否存在语法错误。

回答by Space Ghost

You should always post your error for us to see, otherwise it's much harder to help you.

您应该始终将您的错误发布给我们查看,否则很难为您提供帮助。

I think your error is very simple.

我认为你的错误很简单。

Just go to your main activity, where the java code is and press and hold ShiftCtrlO

只需转到您的主要活动,即 Java 代码所在的位置,然后按住 ShiftCtrlO

Shift Ctrl O

Shift Ctrl O

回答by Elham Gdz

When you are using this code : findViewById(R.id.anything);you must import android.R.id. use this and if you have error again please write it for us.

当您使用此代码时:findViewById(R.id.anything);您必须导入android.R.id. 使用它,如果您再次出错,请为我们写信。

回答by Antonio

If all or a majority of the R's are appearing as errors, go to build and clean project. Now the errors should have disappeared or you should be able to import the R's to your activity.

如果所有或大部分 R 都显示为错误,请转到构建和清理项目。现在错误应该已经消失了,或者您应该能够将 R 导入到您的活动中。