我有6个编辑文本字段,我试图将它们输入到sqlite数据库中,并且我试图将其中的5个解析成双打,但它会抛出该错误而我无法弄清楚该怎么办。
我曾尝试使用valueOf(String)
并进行解析,但似乎都无法正常工作。
我也使用了Long和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方法改为此,但现在它对于字符串和双精度都是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);
}
}
});
}
答案 0 :(得分:1)
getText()
只运行一次,目前是您调用iName
的唯一地方。因此,sExam
,FloatingActionButton
等中的值都是您对这些字段的默认值,而不是点击iName = name.getText().toString();
时的当前值。
相反,将OnClickListener
等行移动到Double.valueOf()
以获取当单击按钮时的当前值。
另请注意,您无法在空字段上使用NumberFormatException
(这是您获得的$ContractorSamAccountNames = Import-Csv "C:\temp\stackoverflow.com_34245941\ContractorDomain.csv" | Select-Object -Property SamAccountName
$i = 0
@(Import-Csv "C:\temp\stackoverflow.com_34245941\employeeList.csv" -Delimiter ",").ForEach({
if ($_.DomainNamesFromAD -eq "ContractorDomain") {
$_ | Add-Member -MemberType "NoteProperty" -Name "SamAccountNameFromAD" -Value ($ContractorSamAccountNames[$i++].SamAccountName) -Force
}
$_
})) | Export-Csv -Path "C:\temp\stackoverflow.com_34245941\employeeList.csv" -Delimiter "," -Force -NoTypeInformation
) - 请考虑将每个电话打包在TextUtils.isEmpty()中以检查是否有任何值一点都不。