无法将数据结果传递给android中的textview

时间:2017-06-10 06:40:38

标签: java android

我创建了一个简单的隐蔽程序,它可以从用户那里获得输入并选择想要隐藏的数字类型。单击确定按钮并在同一活动中返回结果。它看起来像下面的图像。但是,当我单击按钮返回值时,它无法工作或返回结果并显示程序不幸,程序已停止?如何正确地将它传递给textview?在同一活动的结果(textview)中。它看起来像在这张图片中。

enter image description here

下面是我的代码

public class activity_tv9 extends tv9 { 
private int mb;
private int tb;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tv9_act);

TextView tv = (TextView)findViewById(R.id.textView3);
EditText editview = (EditText)findViewById(R.id.editText1);
RadioButton rdb1 = (RadioButton)findViewById(R.id.radio0);
RadioButton rdb2 = (RadioButton)findViewById(R.id.radio1);
final RadioGroup rdg = (RadioGroup)findViewById(R.id.radioGroup1);

Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int value =rdg.getCheckedRadioButtonId();
        rd=(RadioButton)findViewById(value);
         if(rdg.getCheckedRadioButtonId()==-1){
                Toast.makeText(activity_tv9.this,"Please select radio button",
                        Toast.LENGTH_LONG).show();
         }  
         else {
             switch (rd.getText().toString()){
                case "MB":
                    double mb = Integer.parseInt(editview.getText().toString());
                    double val1 = mb*1024;
                    tv.setText(String.valueOf(val1));
                     break;
                case "TB":
                    double tb = Integer.parseInt(editview.getText().toString());
                    double val2 = tb*1024;
                    tv.setText(String.valueOf(val2));
                    break;
                default:
                    Toast.makeText(activity_tv9.this,"No input",
                            Toast.LENGTH_LONG).show();
                }
         }
    }
});

}

1 个答案:

答案 0 :(得分:1)

请将Integer.parseInt(editview.getText().toString())更改为Double.parseDouble(editview.getText().toString()),因为您预计来自double的{​​{1}}值: -

editview

希望这项工作;