after the text2 write l1 and l2 inside the if condition.
if(!text1.getText.tostring("")&&!text2.getText.tostring("")){
l1 = Double.parseDouble(text1.getText().toString());
l2=Double.parseDouble(text2.getText().toString());
}
if edittext is empty it shows -Invalid double: "" error.
On Saturday, June 25, 2016 at 9:36:05 AM UTC+5:30, Samarth Kejriwal wrote:
>
> XML CODE
>
> <?xml version="1.0" encoding="utf-8"?>
>
> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
> xmlns:tools="http://schemas.android.com/tools"
> android:layout_width="match_parent"
> android:layout_height="match_parent"
> android:paddingBottom="@dimen/activity_vertical_margin"
> android:paddingLeft="@dimen/activity_horizontal_margin"
> android:paddingRight="@dimen/activity_horizontal_margin"
> android:paddingTop="@dimen/activity_vertical_margin"
> tools:context="com.example.android.mycalculator.MainActivity">
>
> <TextView
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:text="@string/Operation"
> android:textSize="20sp"/>
> <Spinner
> android:id="@+id/spinner"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:prompt="@string/spinner_title"
> android:layout_marginTop="25dp" />
> <EditText
> android:id="@+id/number1"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:hint="Enter Number"
> android:inputType="number"
> android:layout_below="@+id/spinner"/>
> <EditText
> android:id="@+id/number2"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:hint="Enter Number"
> android:inputType="number"
> android:layout_below="@+id/number1"/>
> <TextView
> android:id="@+id/result"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:layout_marginTop="5dp"
> android:paddingLeft="8dp"
> android:paddingRight="8dp"
> android:text="RESULT"
> android:textColor="#000000"
> android:textSize="20sp"
> android:layout_below="@id/number2"/>
> <View
> android:layout_width="150dp"
> android:layout_height="2dp"
> android:layout_below="@+id/result"
> android:background="@android:color/darker_gray" />
>
>
> </RelativeLayout>
>
> //Java Code
> package com.example.android.mycalculator;
>
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.view.View;
> import android.widget.AdapterView;
> import android.widget.ArrayAdapter;
> import android.widget.EditText;
> import android.widget.Spinner;
> import android.widget.TextView;
>
> public class MainActivity extends Activity implements
> AdapterView.OnItemSelectedListener {
>
> Spinner spinner;
> TextView result;
> private EditText text1;
> private EditText text2;
> double l1,l2;
> private String[] operations = {"Add","Subtract","Multiply","Divide"};
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_main);
>
> spinner = (Spinner) findViewById(R.id.spinner);
> result=(TextView) findViewById(R.id.result);
> spinner.setOnItemSelectedListener(this);
>
> text1 = (EditText) findViewById(R.id.number1);
> text2 = (EditText) findViewById(R.id.number2);
> l1 = Double.parseDouble(text1.getText().toString());
> l2=Double.parseDouble(text2.getText().toString());
>
>
> // Spinner Drop down elements
>
> // Creating adapter for spinner
> ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
> android.R.layout.simple_spinner_item, operations);
>
> // Drop down layout style - list view with radio button
>
> dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
>
> // attaching data adapter to spinner
> spinner.setAdapter(dataAdapter);
> }
>
>
> public String add() {
> double result=l1+l2;
> return Double.toString(result);
> }
> public String sub() {
> double result=l1-l2;
> return Double.toString(result);
> }
> public String mul() {
> double result=l1*l2;
> return Double.toString(result);
> }
>
> public void onItemSelected(AdapterView<?> parent, View view, int
> position, long id) {
> // On selecting a spinner item
> String item = parent.getItemAtPosition(position).toString();
> String selected = (String) spinner.getSelectedItem();
> if(selected.equals(operations[0])) result.setText(add());
> if(selected.equals(operations[1])) result.setText(sub());
> if(selected.equals(operations[2])) result.setText(mul());
>
> }
> public void onNothingSelected(AdapterView<?> arg0) {
> // TODO Auto-generated method stub
> }
>
> }
>
> I am not able to compile my app.It is getting crashed when i run it on my
> device.It is showing the error -Invalid double: "".I am not able to figure
> out the error.PLease Help me.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/805b9af9-b1c3-4327-be96-35d82e01e02b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.