-6

I am trying to get an input from user and then covert it into double and divide it by 1000 and then set the value in a textview but i while running the app, the app closes and says Unfortunately the app has stopped. Here's my MainActivity.java file :

package com.blogspot.techtutorialsajen.androiddevelopmentpractice;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{

private Button btn1;
private EditText get;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1 = (Button)findViewById(R.id.btn1);
    result = (TextView)findViewById(R.id.result);
    get = (EditText)findViewById(R.id.get);

    final double value = Double.parseDouble(get.getText().toString()) / 1000;

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            result.setText(String.valueOf(value));

            }
        });
    }
}

And My activity_main.xml file :

?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<EditText
    android:id="@+id/get"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:hint="@string/enter_a_number"
    android:layout_marginTop="48dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:id="@+id/btn1"
    android:text="@string/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/result"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="28dp" />

<TextView
    android:id="@+id/result"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textSize="20pt"
    android:text="0"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

2 Answers2

1

Well, Your app is crashes because of java.lang.RuntimeException:java.lang.NumberFormatException: Invalid double: "" that means you are trying to convert empty string to Double.

So, Because the EditText is empty your app is crashes and I have re-write your code so that you can copy-paste.

Here is your MainActivity.Java

package com.blogspot.techtutorialsajen.androiddevelopmentpractice;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{

private Button btn1;
private EditText get;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1 = (Button)findViewById(R.id.btn1);
    result = (TextView)findViewById(R.id.result);
    get = (EditText)findViewById(R.id.get);
 btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (get.getText().toString().isEmpty()) {
                    Toast.makeText(MainActivity .this, "Please input something", Toast.LENGTH_LONG).show();
                } else {
                    final double value = Double.parseDouble(get.getText().toString()) / 1000;
                    result.setText(String.valueOf(value));
                }

            }
        });

    }
}

and here is your activity_main.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<EditText
    android:id="@+id/get"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:hint="@string/enter_a_number"
    android:layout_marginTop="48dp"
    android:inputType="number"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:id="@+id/btn1"
    android:text="@string/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/result"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="28dp" />

<TextView
    android:id="@+id/result"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textSize="20pt"
    android:text="0"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

I have added android:inputType="number" property to your EditText so that, now on, it will only take Number from the user.

Ravi Vaniya
  • 1,562
  • 1
  • 17
  • 28
0

From your xml file and activity code, it clearly says your edittext is empty while launching app.

And you are using this code in oncreate()

final double value = Double.parseDouble(get.getText().toString()) / 1000;

which throws java.lang.NumberFormatException: empty String

You can change your code like,

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(!get.getText().toString().isEmpty)
           final double value = Double.parseDouble(get.getText().toString()) / 1000;
           result.setText(String.valueOf(value));

        }
    });
}
Jyoti JK
  • 2,141
  • 1
  • 17
  • 40