没有指定资源类型('id'的值为'@ + id \ st')

时间:2013-11-13 09:47:34

标签: java android r.java-file

我是android编程的新手,我现在正在尝试制作一些按钮,我使用以下代码配置了这些按钮:

MainActivity 类:

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */

    Button st,nd,center;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        st = (Button)findViewById(R.id.st);
        st = (Button)findViewById(R.id.center);
        st = (Button)findViewById(R.id.nd);


    }
}

XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello from animation!!"
    />
<button android:layout_width="100dp"
android:layout_height="50dp"
android:text="1st half"
android:id="@+id\st"
/>
// and the other two points defined the same way
</LinearLayout>

我得到了语法错误:

error: Error: No resource type specified (at 'id' with value '@+id\st').
// and the same error with the other two buttons

提示: R class已导入,可从MainActivity班级访问,但无法读取R.id

3 个答案:

答案 0 :(得分:2)

您的代码中存在一些语法错误

首先使用Button代替按钮。

在id。之后使用/代替\。

<Button android:layout_width="100dp"
android:layout_height="50dp"
android:text="1st half"
android:id="@+id/st"
/>

答案 1 :(得分:1)

您使用button代替Button。它区分大小写。 并在android:id\替换为/

这是正确的代码:

<Button android:layout_width="100dp"
    android:layout_height="50dp"
    android:text="1st half"
    android:id="@+id/st"
/>

用这个代替你的按钮代码,它应该可以正常工作

答案 2 :(得分:0)

button替换为Button,将android:id="@+id\st"替换为android:id="@+id/st"