【发布时间】:2016-06-06 13:15:34
【问题描述】:
我有 6 个编辑文本字段,我正在尝试将它们输入到 sqlite 数据库中,并且我正在尝试将其中的 5 个解析为双精度,但它会引发该错误,我不知道该怎么做。
我尝试使用valueOf(String) 和解析,但似乎都不起作用。
我也使用过 Long 和 Integer,也遇到了同样的问题
代码如下:
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 方法更改为此,但它现在显示为 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);
}
}
});
}
【问题讨论】:
-
您确定您的字段仅包含
doubles 吗? -
你的双打有逗号还是点?
-
您必须先验证您的字段,然后再为字段调用
Double.valueOf -
是的,它们只包含双精度数,我尝试使用整数和长整数,以及同样的问题,它们既没有逗号也没有点
标签: java android android-edittext double numberformatexception