Reputation: 63
Had a little idea for a personal app today. Never ever attempted Android so... any help is appreciated.
What I want is 2 input fields, take those values & * them against each other giving the result after tapping the calc button.
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!
activity_main.xml
<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"
>
<EditText android:id="@+id/fuel"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/fuel" />
<EditText android:id="@+id/numLaps"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/numLaps" />
<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"/>
<EditText
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/output"
android:inputType="number"
/>
</LinearLayout>
MainActivity.java
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);
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">iRacing Fuel Calc</string>
<string name="fuel">Fuel per lap</string>
<string name="numLaps">Number of laps</string>
<string name="calc">Calculate</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_display_message">TEST</string>
<string name="output">0</string>
</resources>
Upvotes: 1
Views: 1686
Reputation: 36449
Your first problem is that you don't actually give a value to output
. In onCreate()
do
output = (EditText)findViewById(R.id.output);
You may also have a problem with the Integer parsing since anything that is not a number will throw a NumberFormatException
. Make sure to only enter numbers or to set the EditText to accept or validate programmatically. One way to validate is by using the try/catch block. E.g.
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)
}
If you want, you can set the EditText to accept in only numbers, via android:inputType
. The docs have more info.
<EditText android:id="@+id/fuel"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/fuel"
android:inputType = "number"/>
Upvotes: 4
Reputation:
The second is:
Integer.parseInt(fuel.getText().toString());
You aren't validating the value of the field. You can get NumberFormaException in action thread, wich leads to app crash
Upvotes: 0