The below is the code I used in my app. The input type for the EditText is number. I receive the data entered into a String variable called 'name'. I try to parse it into an integer variable and this is where I believe that code crashes.
'''
public class MainActivity extends AppCompatActivity {
String name;
int num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt1 = (TextView)findViewById(R.id.txt1);
final TextView txt2 = (TextView)findViewById(R.id.txt2);
final EditText edtxt1 = (EditText)findViewById(R.id.edtxt1);//input type is number
final Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = edtxt1.getText().toString();
num = Integer.parseInt(name);
txt1.setText(num);
}
});
}
}
'''
You have to set String not Int. Change like this
txt1.setText(num.toString());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.