【发布时间】:2014-02-25 15:49:45
【问题描述】:
我正在阅读 Android 的教程,并且我有一个实现抽象类 OnClickListener 的类。问题是,在教程中,当它覆盖方法 onClick 时,它只有一个参数,但我的 Eclipse 显示错误,因为 onClick 方法需要两个参数。
在我的教程错误代码下面,我该如何解决?
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 只需要一个参数,即视图确保您有正确的导入??
-
你一定是导入了错误的包 - onClick() 只需要一个参数。您能向我们展示您的进口产品吗?
-
好的,非常感谢我在导入包时出错了。
-
导入android.view.View;我想就是这样
-
@StefanoMaglione 你有什么改变
import android.content.DialogInterface.OnClickListener??
标签: java android onclicklistener