I have been trying to learn Java for Android development, so I decided to try to make a simple converter application to learn from. At the moment i have a simple UI and I am trying to convert from Celsius to Fahrenheit. the converter will, when working, convert betweem Celsius, Fahrenheit and Kelvin.
When I click the button that is supposed to run the calculation method, i get the error "Unfortunately, Converter has stopped." Below is my code, I have also included the XML for the view as well.
package com.michaelmurphy.converter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Spinner;
public class Temperature extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.temperature_view);
// TODO Auto-generated method stub
}
public void tempCalc()
{
//define variables
float value = 0;
String from = "";//for spinner
String to = "";//for spinner
//get entered value
EditText input = (EditText) findViewById(R.id.editText1);
//convert to string
String enteredValue = input.getText().toString();
//convert string into float
float num = Float.valueOf(enteredValue);
//retrieve the from spinner value
final Spinner fromSpinner = (Spinner) findViewById(R.id.spinner1);
from = fromSpinner.getSelectedItem().toString();
//retrieve the to spinner value
final Spinner toSpinner = (Spinner) findViewById(R.id.spinner1);
to = toSpinner.getSelectedItem().toString();
EditText output = (EditText) findViewById(R.id.textView2);
/*if(from.equals(to)) //the same conversion type
{
//error
}*/
if(from.equals("Celcius"))
{
if(to.equals("Fahrenheit"))
{
value = celToFar(num);
}
else
{
//value = celToKel(num);
}
}
else if(from.equals("Fahrenheit"))
{
if(to.equals("Celcius"))
{
//value = fahToCel(num);
}
else
{
//value = fahToKel(num);
}
}
else //kelvin
{
if(to.equals("Celcius"))
{
//value = kelToCel(num);
}
else
{
//value = kelToFah(num);
}
}
//set the label to variable value
String valueStr = Float.toString(value);//cast float to string
output.setText(valueStr);
}
public float celToFar(float cel)
{
float fah = cel * 9/5 + 32;
return fah;
}
}
View XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/temp_arr" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/temp_arr" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/convertBtn"
android:onClick="tempCalc" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Is anyone able to point out where I am going wrong, I have no idea. Thanks