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 8321715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:07:04+00:00 2026-06-08T23:07:04+00:00

This is a tip calculator app in Java. If both fields are not filled

  • 0

This is a tip calculator app in Java. If both fields are not filled in, it should show an error message in a TextView, but it just freezes the app… any tips?

package tip.calculator;


import android.os.Bundle;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.widget.Button;




public class TipCalculator extends Activity 
{

private Button enter;
EditText myEditField ;
EditText myEditField2;
float percentage = 0;
float percentageInp = 0;
float billAmount = 0;
double output = 0; 
String output1 = "";
Button clearButton ;
TextView textView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myEditField = (EditText) findViewById(R.id.percentText);
    enter = (Button)findViewById(R.id.button1);
    myEditField2 = (EditText) findViewById(R.id.billText);
    clearButton = (Button) findViewById(R.id.clearButton);


    enter.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {

             TextView errors;
             textView = (TextView) findViewById(R.id.textView1);
             errors = (TextView) findViewById(R.id.errorText);    




             if(myEditField.getText().equals("")){
                 errors.setText("Percent must be filled in");


             }

             if(myEditField.getText().equals("")){
                 errors.setText("Bill Amount must be filled in");

             }




            percentageInp = Float.parseFloat(myEditField.getText().toString());
            billAmount = Float.parseFloat(myEditField2.getText().toString());

            percentage = ((float)percentageInp /100);

            output = (double)(billAmount * percentage);

            double result = output * 100;
            result = Math.round(result);
            result = result / 100;

            output1 = Double.toString(result);

            textView.setText(output1 + " $");

        }
    });

    clearButton.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            percentage = 0;
            output = 0;
            output1 = "";

            TextView textView = (TextView)findViewById(R.id.errorText);
            textView.setText("");

            TextView textView2 = (TextView)findViewById(R.id.percentText);
            textView2.setText("");

            TextView textView3 = (TextView)findViewById(R.id.billText);
            textView3.setText("");


            TextView textView1 = (TextView)findViewById(R.id.textView1);
            textView1.setText("");


            percentageInp = 0;
            billAmount = 0;

            myEditField.clearComposingText();
            myEditField2.clearComposingText();



            return;
        }


    });
}*

}

here is the layout xml(provided by request)…

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/errorText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="onEnterClick" >

    <EditText
        android:id="@+id/billText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="Bill Amount"
        android:inputType="numberDecimal|textAutoComplete"
        android:singleLine="true" />

    <EditText
        android:id="@+id/percentText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/billText"
        android:layout_below="@+id/billText"
        android:ems="10"
        android:hint="Percent"
        android:inputType="number"
        android:singleLine="true" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/percentText"
        android:layout_centerHorizontal="true"
        android:text="Calculate" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/percentText"
        android:layout_toRightOf="@+id/billText"
        android:text="$"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/percentText"
        android:layout_alignLeft="@+id/textView2"
        android:text="%"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/clearButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="Clear" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView3"
        android:layout_below="@+id/clearButton"
        android:text="Solution Will Appear Here"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/errorText"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

here is the LogCat output (errors only)

08-01 20:49:08.424: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2
08-01 20:49:08.689: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2
08-01 20:49:10.844: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2
08-01 20:49:12.074: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2
08-01 20:49:12.254: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2
08-01 20:49:16.649: E/AndroidRuntime(13643): FATAL EXCEPTION: main
08-01 20:49:16.649: E/AndroidRuntime(13643): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
08-01 20:49:16.649: E/AndroidRuntime(13643):    at tip.calculator.TipCalculator$2.onClick(TipCalculator.java:91)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at android.view.View.performClick(View.java:3620)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at android.view.View$PerformClick.run(View.java:14292)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at android.os.Handler.handleCallback(Handler.java:605)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at android.os.Looper.loop(Looper.java:137)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at android.app.ActivityThread.main(ActivityThread.java:4507)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-01 20:49:16.649: E/AndroidRuntime(13643):    at dalvik.system.NativeStart.main(Native Method)
  • 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-08T23:07:06+00:00Added an answer on June 8, 2026 at 11:07 pm

    From your LogCat, I get it there is no TextView with the id errorText, but you don’t see any errors at compiling time, as you have a RelativeLayout with this id; but you get the ClassCastException error at runtime.

     errors = (TextView) findViewById(R.id.errorText);
    

    Also, always use ""..equals(myEditField.getText()) instead myEditField.getText().equals("") (that is, the hardcoded value first).

    Otherwise, if your EditText is empty, myEditField.getText() will return null, so you’ll have an error trying to use the equals() method on null.

    Alternatively, you can use myEditField.getText().length() = 0 to check if the EditText is empty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried this Java Tip, but was unsuccessful. And by unsuccessful, I mean that
Can i show a Tool tip like this: Also, I want to show this
I've been trying every tip provided on this site, but none worked, so I'm
Well I am developing a simple Tip Calculator app as a part of a
I've seen this tip on DevX which should ' account for most of the
a simple android app to calculate tips, i cant figure out this error. the
I am putting a label on a UIToolbar (per this tip: Adding a UILabel
I'm writing this URL in my browser: https://landfill.bugzilla.org/bugzilla-tip/post_bug.cgi?Bugzilla_login=mymail@hotmail.com&Bugzilla_password=password&product=WorldControl&version=1.0&component=WeatherControl&rep_platform=All&op_sys=All&priority=P2&bug_severity=normal&target_milestone=World%202.0&bug_status=CONFIRMED&assigned_to=somemail@hotmail.com&short_desc=bla&form_name=enter_bug&token=someToken or more clean: https://landfill.bugzilla.org/bugzilla-tip/post_bug.cgi ?Bugzilla_login=mymail@hotmail.com &Bugzilla_password=password
I have this html line: <div class=hide-btn top tip-s original-title=Close sidebar></div> And when a
This should be a simple one: I have an observableArray object called To in

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.