title: Calculation using Android
body: <p>Had a little idea for a personal app today. Never ever attempted Android so... any help is appreciated.</p>
  
  <p>What I want is 2 input fields, take those values &amp; * them against each other giving the result after tapping the calc button. </p>
  
  <p>It looks OK to me, but like I said I am not all that good with Java and new to Android. My issue is when I tap the Calc button it force closes/stops working. Thanks in advance!</p>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;LinearLayout 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"
      tools:context=".MainActivity" 
  
      &gt;
  
      &lt;EditText android:id="@+id/fuel"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:hint="@string/fuel" /&gt;
  
      &lt;EditText android:id="@+id/numLaps"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:hint="@string/numLaps" /&gt;
  
      &lt;Button
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="wrap_content" 
          android:id="@+id/calc"
          android:text="@string/calc"
          android:onClick="calc"/&gt;
  
      &lt;EditText
          android:layout_weight="1"
          android:layout_width="0dp" 
          android:layout_height="wrap_content" 
          android:id="@+id/output"
          android:inputType="number"
          /&gt;
  
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package com.example.iracingfuelcalc;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  
  public class MainActivity extends Activity implements android.view.View.OnClickListener {
  
  
      Button calc;
      EditText numLaps,fuel, output;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          fuel = (EditText)findViewById(R.id.fuel);
          numLaps = (EditText)findViewById(R.id.numLaps);
          output = (EditText)findViewById(R.id.output);
          calc = (Button)findViewById(R.id.calc);
  
  
          calc.setOnClickListener(this);  
  
      }
  public void onClick(View arg0){
  
      int fuelValue = -1;
      try {
      fuelValue = Integer.parseInt(fuel.getText().toString());
      }
      catch (NumberFormatException e)
      {
      //you don't have to do much here since fuelValue already has a default value (-1)
      }
  
      int lapsValue = -1;
      try {
          lapsValue = Integer.parseInt(numLaps.getText().toString());
      }
      catch (NumberFormatException e)
      {
  
      }
  
      int result = 0;
  
  
  
          result = fuelValue * lapsValue; 
  
      result += Integer.parseInt(output.getText().toString());
      output.setText("" + result);
  
  }
  
  }
  </code></pre>
  
  <p><strong>strings.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
  
      &lt;string name="app_name"&gt;iRacing Fuel Calc&lt;/string&gt;
      &lt;string name="fuel"&gt;Fuel per lap&lt;/string&gt;
      &lt;string name="numLaps"&gt;Number of laps&lt;/string&gt;
      &lt;string name="calc"&gt;Calculate&lt;/string&gt;
      &lt;string name="menu_settings"&gt;Settings&lt;/string&gt;
      &lt;string name="title_activity_display_message"&gt;TEST&lt;/string&gt;
      &lt;string name="output"&gt;0&lt;/string&gt;
  
  &lt;/resources&gt;
  </code></pre>