如何将String转换为Double

时间:2015-05-12 12:41:38

标签: android

`公共类GPA扩展了活动{

private Spinner GradeSpinner1;

private TextView Total1;


private EditText EditCH1;


double chEntered;
double totalPoints;
String grade;
String groupChoice;

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

    groupChoice = GradeSpinner1.getSelectedItem().toString();
    GradeSpinner1 = (Spinner) findViewById(R.id.spGP1);


    Total1 = (TextView) findViewById(R.id.editTotal1);


    EditCH1 = (EditText) findViewById(R.id.editCH1);

    List<String> list = new ArrayList<String>();
    list.add("--SELECT--");
    list.add("A+");
    list.add("A");
    list.add("A-");
    list.add("B+");
    list.add("B");
    list.add("B-");
    list.add("C+");
    list.add("C");
    list.add("C-");
    list.add("D+");
    list.add("D");
    list.add("F");



    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    GradeSpinner1.setAdapter(dataAdapter);

    setUpListeners();
}



private void setUpListeners() {

    GradeSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {

            String credit = (EditCH1.getText().toString()); 

            Double chEntered = Double.parseDouble(credit);

              if(grade.equals("A")){

                  Double editTotal1 = chEntered * 4.00;

                  Total1.setText(editTotal1.toString());

              }



        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }



        });

} }

`我想为微调器编写if / else语句并编辑文本。这就是我所做的。没有错误bt cnt运行。这是我的java代码。任何人都可以帮助我吗?请。先感谢您。

1 个答案:

答案 0 :(得分:0)

不知道问题出在哪里,你必须添加堆栈跟踪。但是你的代码必须在onItemSelected:

中进行改造
  String grade = parent.getItemAtPosition(pos).getText().toString(); //first mistake

  String editTextText = EditCH1.getText().toString();

  //check if String is null  or empty before parsing. otherwise You will get an error if it is null or empty

  if(editTextText!=null && !editTextText.equals("")){

      chEntered = Double.parseDouble(editTextText);
  }

  if(grade.equals("A")){

      totalPoints = chEntered*4.0;
      String points = Double.toString(totalPoints);

      Total1.setText(points);

  }


    }

你的EditText应该输入xml layout android:inputType="numberDecimal"

中的输入

如果字符串是数字,另一个简单的测试是使用TextUtils检查:

   boolean digitOnly = android.text.TextUtils.isDigitsOnly(editTextText);

但也许这对十进制数不起作用。有一个非常简单的解决方案:

     private boolean isNumber(String numStr)  
{  
  try  
  {  
    double d = Double.parseDouble(numStr);  
  }  
  catch(NumberFormatException nfe)  
  {  
    return false;  
  }  
  return true;  
}

你的“应用程序停止了”描述可能是由于一切造成的!有这么多的可能性......重要的是看到你的堆栈跟踪,你必须发布一部分代码而不是链接到图片。