当我遇到此错误时,我在android中使用RadioGroup, 我无法找出原因:
Process: com.example.vinicius.crudnotas, PID: 16753
java.lang.NumberFormatException: Invalid double: ""
以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inserir_notas);
Button botao = (Button)findViewById(R.id.button);
botao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Controller crud = new Controller(getBaseContext());
EditText nota1Id = (EditText)findViewById(R.id.editText);
EditText nota2Id = (EditText)findViewById(R.id.editText3);
String nota1String = nota1Id.getText().toString();
String nota2String = nota2Id.getText().toString();
double nota1 = Double.parseDouble(nota1String);
double nota2 = Double.parseDouble(nota2String);
double notaFinal = (nota1+nota2)/2;
String resultado;
String escolha;
RadioGroup group = (RadioGroup)findViewById(R.id.radio_materia);
int op = group.getCheckedRadioButtonId();
if(op==R.id.radio_php) {
escolha = "php";
}else if(op==R.id.radio_java) {
escolha = "java";
}else {
escolha = "Escolha uma opcao";
}
Context context = getApplicationContext();
CharSequence text = escolha;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} }
我需要获取所选RadioGroup的文本才能进入银行
答案 0 :(得分:0)
错误是nota1String
或nota2String
不包含任何有效的Double
值或其字符串表示形式。由于这些是用户输入字段,因此请确保处理用户未在其中放置任何值的情况。以下是我认为您可以在onClick
函数中进行的一些小改动:
try{
String nota1String = nota1Id.getText().toString();
String nota2String = nota2Id.getText().toString();
//here I check if the string is empty, and then put "0" instead.
double nota1 = Double.parseDouble((!nota1String.trim().isEmpty()? nota1String: "0"));
//again, here I check if the string is empty, and then put "0" instead.
double nota2 = Double.parseDouble((!nota2String.trim().isEmpty()? nota2String: "0"));
double notaFinal = (nota1+nota2)/2;
}
catch(Exception e){
//something went wrong above...
}
答案 1 :(得分:0)
试试这段代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inserir_notas);
Button botao = (Button)findViewById(R.id.button);
botao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Controller crud = new Controller(getBaseContext());
EditText nota1Id = (EditText)findViewById(R.id.editText);
EditText nota2Id = (EditText)findViewById(R.id.editText3);
String nota1String = nota1Id.getText().equals("")?"0":nota1Id.getText().toString();
String nota2String = nota2Id.getText().equals("")?"0":nota2Id.getText().toString();
double nota1 = Double.parseDouble(nota1String);
double nota2 = Double.parseDouble(nota2String);
double notaFinal = (nota1+nota2)/2;
String resultado;
String escolha;
RadioGroup group = (RadioGroup)findViewById(R.id.radio_materia);
int op = group.getCheckedRadioButtonId();
if(op==R.id.radio_php) {
escolha = "php";
}else if(op==R.id.radio_java) {
escolha = "java";
}else {
escolha = "Escolha uma opcao";
}
Context context = getApplicationContext();
CharSequence text = escolha;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
希望它会对你有所帮助:)。