0
I’m not getting this check I’ve tried everything, but always gives me this mistake:
java.lang.Numberformatexception: Invalid double: ""
If anyone can help, thank you.
package com.calculadora;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private Button btncalc, btnlimpar;
private AppCompatEditText edn1, edn2;
private AppCompatTextView txtResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btncalc = (Button) findViewById(R.id.calcular);
btnlimpar = (Button) findViewById(R.id.limpar);
edn1 = (AppCompatEditText)findViewById(R.id.n1);
edn2 = (AppCompatEditText)findViewById(R.id.n2);
txtResult = (AppCompatTextView)findViewById(R.id.result);
btncalc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double n1 = Double.parseDouble(edn1.getText().toString());
double n2 = Double.parseDouble(edn2.getText().toString());
double res = (n1*2 + n2*3) / 5;
if(res >= 6){
txtResult.setText("Voce Foi Aprovado Parabens Sua nota é: " + res);
txtResult.setTextColor(Color.parseColor("#3F51B5"));
edn1.setText("");
edn2.setText("");
}else if(res < 6){
txtResult.setText("Voce Foi Reprovado Estude Mais da Próxima Sua nota é: " + res);
txtResult.setTextColor(Color.parseColor("#F44336"));
edn1.setText("");
edn2.setText("");
/*Linha Com Erro é essa*/ }else if(TextUtils.isEmpty(edn1.getText().toString()) && TextUtils.isEmpty(edn2.getText().toString())) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Aviso");
dialog.setMessage("Campo Vazio!");
dialog.setNeutralButton("OK", null);
dialog.show();
}
}
});
btnlimpar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edn1.setText("");
edn2.setText("");
txtResult.setText("");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.action_sobre){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Sobre o App");
dialog.setMessage(R.string.mensage);
dialog.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent it = new Intent();
it.setData(Uri.parse("http://www.facebook.com/diogosilvam"));
startActivity(it);
}
});
dialog.show();
}
return super.onOptionsItemSelected(item);
}
}
XML:
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
android:padding="10dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_marginTop="50dp"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="4dp"
app:cardElevation="4dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/logo" />
<android.support.v7.widget.AppCompatTextView
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="48dp"
android:text="Preencha as suas notas para calcular sua média"
android:textColor="#000"
android:textSize="18sp" />
<android.support.v7.widget.AppCompatTextView
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:paddingLeft="14dp"
android:text="N1 (0-10)"
android:textColor="#000"
android:textSize="18sp" />
<android.support.v7.widget.AppCompatEditText
android:textStyle="bold"
android:id="@+id/n1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:hint="0.0"
android:numeric="decimal"
android:paddingLeft="14dp"
android:textColor="#000"
android:textColorHint="#919191"
android:maxLength="3"/>
<android.support.v7.widget.AppCompatTextView
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:paddingLeft="14dp"
android:text="N2 (0-10)"
android:textColor="#000"
android:textSize="18sp" />
<android.support.v7.widget.AppCompatEditText
android:textStyle="bold"
android:maxLength="3"
android:isScrollContainer="false"
android:id="@+id/n2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:hint="0.0"
android:numeric="decimal"
android:paddingLeft="14dp"
android:textColor="#000"
android:windowSoftInputMode="adjustPan"
android:textColorHint="#919191" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:textStyle="bold"
android:id="@+id/calcular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="210dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:stateListAnimator="@anim/btn_anim"
android:text="Calcular" />
<Button
android:textStyle="bold"
android:id="@+id/limpar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="12dp"
android:layout_marginTop="210dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:stateListAnimator="@anim/btn_anim"
android:text="Limpar" />
</LinearLayout>
<android.support.v7.widget.AppCompatTextView
android:textStyle="bold"
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="270dp"
android:paddingLeft="14dp"
android:text="Resultado"
android:textColor="#000"
android:textSize="18sp" />
</android.support.v7.widget.CardView>
</LinearLayout>
On which line of code bursts this exception?
– user28595
Line 64 friend!
– Diogo Silva
And how do we know which line is 64 in the code? It would be interesting if you pointed out in the question what line is this.
– user28595
Post xml too, I think the problem is time to recover the view values, with findViewById.
– Geferson
I’ve already put Take a look there
– Diogo Silva
The error occurs because you are passing an empty string value to parseDouble, and it cannot convert.
– user28595
What I want is for him to check when it’s empty show me an Alert that editText has no value
– Diogo Silva
So you are doing it wrong. First do the if that checks if the fields are empty, then try to parse. You’re first trying to parse and then check if it’s empty.
– user28595
OK I’ll try here wlw
– Diogo Silva
How to mark as solved?
– Diogo Silva
To mark as solved you need an answer. You can answer your question yourself.
– Caique Oliveira
Guys I thank the most comments I’ve managed to wlw even!!!
– Diogo Silva
@Diogosilva You can answer your question by giving the solution to it, and not just by saying that it solved, rs.. The solution is important in case someone comes to this topic with the same doubt that you learn how to solve the problem. After answered you can mark it as accept by clicking on next to it, leaving it green.
– Math