2013-05-13 120 views
0

我想調試我onClick方法在我SaveActivityEclipse中的Android - 調試器不會在斷點處停止對OnClick方法

public class SaveActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_save); 

    getActionBar().setDisplayHomeAsUpEnabled(true); 
} 

// For the Back Button 
// (http://developer.android.com/training/implementing-navigation/ancestral.html) 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     // This is called when the Home (Up) button is pressed 
     // in the Action Bar. 
     Intent parentActivityIntent = new Intent(this, MainActivity.class); 
     parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(parentActivityIntent); 
     finish(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public void onClick(View view) { 

    switch (view.getId()) { // <-- On this line is my Breakpoint 
    case R.id.location_save: 

     EditText formName = (EditText) findViewById(R.id.location_name); 
     String locationName = formName.getText().toString(); 
     EditText formDesc = (EditText) findViewById(R.id.location_desc); 
     String locationDesc = formDesc.getText().toString(); 
     // Example for New York 
     String locationLatitude = "40.714353"; 
     String locationLongitude = "-74.005973"; 

     DatabaseHandler db = new DatabaseHandler(this); 
     Location newLocation = new Location(locationName, locationDesc, 
       locationLatitude, locationLongitude); 
     db.addLocation(newLocation); 
     Intent myIntent = new Intent(SaveActivity.this, DetailActivity.class); 
     myIntent.putExtra("id", newLocation.getId()); 
     SaveActivity.this.startActivity(myIntent); 

    } 
} 

}

現在我增加了一個斷點上switch -Statemant onClick方法。然後我在activity_save.xml點擊這個按鈕:

<Button 
    android:id="@+id/location_save" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/location_save" > 
</Button> 

所以應該在我的斷點處停止,但沒有happend。
我錯了什麼?

回答

2

在按鍵佈局添加這方法綁定到其的onClick:

android:onClick="onClick" 
+0

哦,很容易!完善!非常感謝!將在5分鐘內接受:) – 2013-05-13 07:51:00

+0

或者在視圖上調用setOnClickListener並將其傳遞給onClickListener。如果沒有這些方法之一,它不會知道將onClickListener連接到onClick函數 - android爲此使用對象,它不會執行自動連接 – 2013-05-13 07:51:10

相關問題