我正在閱讀Android的教程,我有一個實現抽象類OnClickListener的類。問題是,在教程中,當它重寫方法onClick時,它只有一個參數,但我的eclipse顯示錯誤,因爲onClick方法需要兩個參數。實現OnClickListener
下面我錯了代碼的教程,我該如何解決它?
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.form_button);
button.setOnClickListener((android.view.View.OnClickListener) this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.form_button:
final EditText edit_name = (EditText) findViewById(R.id.edit_name);
final EditText edit_lastname = (EditText) findViewById(R.id.edit_lastname);
Bundle bundle = new Bundle();
bundle.putString("name", edit_name.getText().toString());
bundle.putString("lastname", edit_lastname.getText().toString());
Intent form_intent = new Intent(getApplicationContext(), Form.class);
form_intent.putExtras(bundle);
startActivity(form_intent);
break;
}
}
}
onClick只需要一個參數,即視圖確定您有正確的導入? – Raghunandan
您必須導入錯誤的包 - onClick()只需要一個參數。你能告訴我們你的進口嗎? – ucsunil
好的,非常感謝我在導入包中錯誤。 –