gpt4 book ai didi

android - 由于 Toast 和 OnClickListener 组合无效导致的错误

转载 作者:IT老高 更新时间:2023-10-28 21:35:55 26 4
gpt4 key购买 nike

我正在尝试在 OnCLickListener 中使用 Toast。我的代码触发了以下错误:

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();



}
});

最佳答案

正如 The Kenny 所说,this 指的是 View.OnClickListener 而不是您的 Activity。将此更改为 MyActivity.this

例如,

public class MyActivity extends Activity {
// ... other code here
Toast.makeText(MyActivity.this, Lname, Toast.LENGTH_SHORT).show();

关于android - 由于 Toast 和 OnClickListener 组合无效导致的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4531539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com