Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9036759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:04:48+00:00 2026-06-16T09:04:48+00:00

Had a little idea for a personal app today. Never ever attempted Android so…

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:04:49+00:00Added an answer on June 16, 2026 at 9:04 am

    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"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning C# and WPF and had an idea for a little utility.
I've been using prepared statements for a little while now and I've never had
I am a little bit new to the PHP/MYSQL arena, and had an idea
I'm a little bit confused about how to approach this. I had never used
So I'm trying to make myself a little alarm clock app to learn android.
I had an idea for an app that would require me to implement these
Ok, so I had this neat little idea the other night to create a
I had this weird problem a little while ago and haven't been able to
A little knowledge can be a dangerous thing. Now that I've had a run
I am a little rusty with QT but I've had to use it for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.