2012-07-10 95 views
-3

我在我的私人View.OnClickListner onSave = new View.OnclickListner()中收到「ID無法解析或不是字段」的錯誤。獲取錯誤「ID無法解析或不是字段」

我收到錯誤「參數onSave的非法修飾符;只允許final」。

ID的activity.java文件和佈局文件也是一樣的。

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> 
+1

在未來,請您多花時間寫或格式化您的文章。格式化幫助可以在這裏找到(http://stackoverflow.com/editing-help) – Matt 2012-07-10 10:00:13

回答

2

你的按鈕的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); 
+0

thnx,但是仍然在同一行上留下一個錯誤R無法解析爲變量。 n我已經丟失了我的R.java文件 – user1458650 2012-07-10 09:51:33

+0

只需要幾秒鐘就可以重新生成R類。如果不是,請清理並重建項目。 – MByD 2012-07-10 09:52:20

+0

@BinyaminSharet:他沒有關閉Edittext標籤,因爲問題 – 2012-07-10 09:54:38

相關問題