i'm trying to make a temperature converter app for an android. i want to use 2 spinners ("From" and "To"). with 3 items, Celsius, Kelvin and Fahrenheit. i also added one text field in which user will put a number in. at the bottom there is a button "Convert" and one textview for a result. now what i need is, to make when "Convert" button is pressed textview is replaced by result but depending on selected items in 2 spinners. for example if selected items are from celsius to fahrenheit it should calculate result like this: 9/5*(number written by user)+32 so my question is how do i do that, i think i should use "if" but i'm not sure how, since items aren't defined by anything :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="From">
<item>Celsius</item>
<item>Kelvin</item>
<item>Fahrenheit</item>
</string-array>
</resources>
btw this is xml file in values which i made for spinner obviously. and here is my MainActivity:
package com.usc.uscconverter;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Spinner sp, sp2;
TextView textFrom, textTo, textDegree, textResult;
EditText editText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> ar= ArrayAdapter.createFromResource(this, R.array.From, android.R.layout.simple_list_item_1);
ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp.setAdapter(ar);
sp2=(Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> ar2= ArrayAdapter.createFromResource(this, R.array.To, android.R.layout.simple_list_item_1);
ar2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(ar2);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener(){
public void onClick(View v){
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
EDIT i edited my main activity and added this:
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if (sp.getSelectedItem().toString().equals("Celsius") && sp2.getSelectedItem().toString().equals("Fahrenheit")){
editText1 = (EditText)findViewById(R.id.editText1);
num = editText1.getText().toString();
int d = 9/5;
int n = Integer.parseInt(num);
int r = d * n + 32;
textResult.setText(String.valueOf(r));
}
}
});
no errors though, but when i launch the app it crashes, and in logcat it says like this:
11-29 18:42:24.890: D/dalvikvm(846): Not late-enabling CheckJNI (already on)
11-29 18:42:36.830: I/Choreographer(846): Skipped 165 frames! The application may be doing too much work on its main thread.
11-29 18:42:37.840: D/gralloc_goldfish(846): Emulator without GPU emulation detected.
11-29 18:42:41.740: D/dalvikvm(846): GC_FOR_ALLOC freed 113K, 6% free 2971K/3156K, paused 39ms, total 46ms
11-29 18:42:41.980: I/Choreographer(846): Skipped 75 frames! The application may be doing too much work on its main thread.
11-29 18:42:43.240: I/Choreographer(846): Skipped 118 frames! The application may be doing too much work on its main thread.
11-29 18:42:44.190: I/Choreographer(846): Skipped 102 frames! The application may be doing too much work on its main thread.
11-29 18:42:45.330: I/Choreographer(846): Skipped 79 frames! The application may be doing too much work on its main thread.
11-29 18:42:45.630: I/Choreographer(846): Skipped 57 frames! The application may be doing too much work on its main thread.
11-29 18:42:50.050: D/AndroidRuntime(846): Shutting down VM
11-29 18:42:50.050: W/dalvikvm(846): threadid=1: thread exiting with uncaught exception (group=0xb2a42ba8)
11-29 18:42:50.060: E/AndroidRuntime(846): FATAL EXCEPTION: main
11-29 18:42:50.060: E/AndroidRuntime(846): Process: com.usc.uscconverter, PID: 846
11-29 18:42:50.060: E/AndroidRuntime(846): java.lang.NullPointerException
11-29 18:42:50.060: E/AndroidRuntime(846): at com.usc.uscconverter.MainActivity$1.onClick(MainActivity.java:49)
11-29 18:42:50.060: E/AndroidRuntime(846): at android.view.View.performClick(View.java:4438)
11-29 18:42:50.060: E/AndroidRuntime(846): at android.view.View$PerformClick.run(View.java:18422)
11-29 18:42:50.060: E/AndroidRuntime(846): at android.os.Handler.handleCallback(Handler.java:733)
11-29 18:42:50.060: E/AndroidRuntime(846): at android.os.Handler.dispatchMessage(Handler.java:95)
11-29 18:42:50.060: E/AndroidRuntime(846): at android.os.Looper.loop(Looper.java:136)
11-29 18:42:50.060: E/AndroidRuntime(846): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-29 18:42:50.060: E/AndroidRuntime(846): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 18:42:50.060: E/AndroidRuntime(846): at java.lang.reflect.Method.invoke(Method.java:515)
11-29 18:42:50.060: E/AndroidRuntime(846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-29 18:42:50.060: E/AndroidRuntime(846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-29 18:42:50.060: E/AndroidRuntime(846): at dalvik.system.NativeStart.main(Native Method)
11-29 18:42:54.150: I/Process(846): Sending signal. PID: 846 SIG: 9