简体   繁体   English

EditText-不能在原始类型int上调用getText()

[英]EditText -Cannot invoke getText() on the primitive type int

I am new to android programming. 我是android编程的新手。 And I wanted to set the value of x in edittext and display it on the toast through edittext.The main problem is in x.getText().It gives an error Cannot invoke getText() on the primitive type int. 我想在edittext中设置x的值并通过edittext在烤面包上显示它。主要问题在x.getText()中,它给出了一个错误:无法在基本类型int上调用getText()。 plz can anyone help me its an emergency..? 谁能帮我一个急事..?

Button butt;
EditText edi;
final int x=10;

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

    edi=(EditText)findViewById(R.id.ed);
    final int y = Integer.parseInt(x.getText().toString());
    edi.setText(" "+y);
    butt=(Button)findViewById(R.id.but);
    butt.setOnClickListener(this);
}
@Override
public void onClick(View v) {

    String s= edi.getText().toString();

    Toast.makeText(this, ""+s, 0).show();

}

try this code: 试试这个代码:
in oncreate method: 在oncreate方法中:

    edi=(EditText)findViewById(R.id.ed);
    final int y = x;
    edi.setText(" "+y);
    butt=(Button)findViewById(R.id.but);
    butt.setOnClickListener(this);

The reason for getting error is you are tried to convert the int as int actually the function Integer.parseInt(String s) is used to convert the string to int.in your case it is in int type. 出现错误的原因是您尝试将int转换为int,实际上函数Integer.parseInt(String s)用于将字符串转换为int。在这种情况下,它是int类型的。 So that you got an error. 这样你就出错了。

you should be using 你应该使用

final int y = Integer.parseInt(edi.getText().toString());

instead of 代替

final int y = Integer.parseInt(x.getText().toString());

PROBLEM 问题

final int y = Integer.parseInt(x.getText().toString());    // delete this line, getText() is used on objects, to fetch the text from it. 

CHANGE THIS 改变这个

edi.setText(" "+y);

TO

edi.setText(" "+x);

Instead of " " + , it's good practice to use String.valueOf(int) . 最好使用String.valueOf(int)代替" " +

edi.setText(String.valueOf(x));

you got error at this line 你在这一行有错误

final int y = Integer.parseInt(x.getText().toString());

because x is Integer there is no getText() method for Integer 因为x是Integer,所以没有用于Integer的getText()方法

you use this 你用这个

edi.setText(" "+x);
final int y = Integer.parseInt(edi.getText().toString());

or 要么

final int y = x;

edi.setText(" "+y);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM