我是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
。
答案 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"