我正在尝试使用Toast
inside OnCLickListener
。我的代码触发以下错误:
The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
这是我的代码:
Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText name = (EditText)findViewById(R.id.name);
String Lname = name.getText().toString();
Toast.makeText(this, Lname, Toast.LENGTH_SHORT).show();
}
});
正如肯尼所说,this
指的是View.OnClickListener
而不是你的Activity
。将其更改为MyActivity.this
.
例如,
public class MyActivity extends Activity {
// ... other code here
Toast.makeText(MyActivity.this, Lname, Toast.LENGTH_SHORT).show();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)