java.lang.NumberFormat异常:无效双精度:" "编辑文本



我有6个编辑文本字段,我正试图将它们输入到sqlite数据库中,并试图将其中5个字段解析为double,但它会引发错误,我不知道该怎么办。我尝试过使用valueOf(String)和解析,但似乎都不起作用。

我也用过Long's和Integers,也遇到过同样的问题

这是代码:

public class EditClass extends AppCompatActivity {
EditText name, exam, quiz, assignment, participation, lab;
String iName, sExam, sQuiz, sAss, sPart, sLab ;
double iExam, iQuiz, iLab, iAss, iPart;
FloatingActionButton fab;
Database database;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_class);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    name = (EditText) findViewById(R.id.class_name);
    exam = (EditText) findViewById(R.id.exam_weight);
    quiz = (EditText) findViewById(R.id.quiz_weight);
    assignment = (EditText) findViewById(R.id.assignment_weight);
    participation = (EditText) findViewById(R.id.participation_weight);
    lab = (EditText) findViewById(R.id.lab_weight);
    iName = name.getText().toString();
    sExam = exam.getText().toString();
    sQuiz = quiz.getText().toString();
    sAss = assignment.getText().toString();
    sPart = participation.getText().toString();
    ImageView create = new ImageView(this);
    create.setImageDrawable(getResources().getDrawable(R.drawable.checkmark));
    fab = new FloatingActionButton.Builder(this).setContentView(create).build();
    fab.setBackground(getResources().getDrawable(R.drawable.button_action_lightblue_ztek));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    addData();
}
public void addData(){
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                /**
                 * Errors with parsing into integer form, comes up with NumberFormatException
                 */
               database.insertData(iName,
                        iExam = Double.valueOf(sExam),
                        iQuiz = Double.valueOf(sQuiz),
                        iLab = Double.valueOf(sLab),
                        iAss = Double.valueOf(sAss),
                        iPart = Double.valueOf(sPart));
                        Toast.makeText(EditClass.this, "Data was Successfully Inserted", Toast.LENGTH_SHORT).show();
            }catch (Exception e){
               String error= e.toString();
                Toast.makeText(EditClass.this, "Data was Not Inserted", Toast.LENGTH_SHORT).show();
                System.out.println(error);
            }
        }
    });
}}

我现在已经把addData方法改成了这个,但对于字符串和double,它现在都是null。

方法如下:

public void addData(){
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                /**
                 * Errors with parsing into integer form, comes up with NumberFormatException
                 */
               database.insertData(name.getText().toString(),
                        Double.parseDouble(exam.getText().toString()),
                        Double.parseDouble(quiz.getText().toString()),
                        Double.parseDouble(lab.getText().toString()),
                        Double.parseDouble(assignment.getText().toString()),
                     Double.parseDouble(participation.getText().toString()));

                        Toast.makeText(EditClass.this, "Data was Successfully Inserted", Toast.LENGTH_SHORT).show();
            }catch (Exception e){
               String error= e.toString();
                Toast.makeText(EditClass.this, "Data was Not Inserted", Toast.LENGTH_SHORT).show();
                System.out.println(error);
            }

        }
    });
}

onCreate()只运行一次,目前是您唯一调用getText()的地方。因此,iNamesExam等中的值都是这些字段的默认值,而不是单击FloatingActionButton时的当前值。

相反,当单击按钮时,将iName = name.getText().toString();等行移动到OnClickListener中以获得当前值。

还要注意,不能在空字段上使用Double.valueOf()(这就是您得到的NumberFormatException)-考虑将每个调用包装在TextUtils.isEmpty()中以检查是否有任何值。

相关内容

最新更新