<分区>
我有 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);
}
}
});
}