获取错误"id can not be resolved or is not a field"



我在我的私有View.OnClickListner onSave = new View.OnclickListner()中收到一个错误"id无法解析或不是字段"。

我得到错误"参数onSave的非法修饰符;只允许使用final"。

activity.java文件和布局文件中的ID也是相同的。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button save = (Button) findViewById(R.id.save);
    save.setOnClickListener(onSave);
    private View.OnClickListener onSave  = new View.OnClickListener() {
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            EditText name = (EditText)findViewById(R.id.name);
            EditText address = (EditText)findViewById(R.id.add);

布局代码

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical"
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="1">
            <TableRow
                <TextView android:text="Name    :        " />
                <EditText android:id="@+id/name"></EditText>
             ></TableRow>
            <TableRow
                <TextView android:text="Address    :    "/>
                <EditText android:id="@+id/add"</EditText>
            ></TableRow>
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Save" />
        ></TableLayout>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    </LinearLayout>

您的按钮id是button1,而不是save,因此此行:

Button save =(Button) findViewById(R.id.save);

应该是

Button save =(Button) findViewById(R.id.button1);

或者,如果你想更清楚,你可以把它改为:

android:id="@+id/save_button"

Button save =(Button) findViewById(R.id.save_button);

相关内容

最新更新