Question

I am novice programmer and have been using Eclipse for about 3 months now in a college course. I am trying to make a simplified calculator that will calculate menu price costs and return the ideal values for the menu price and cost percentage. The program says there are no errors but if I've learned anything through programming, its that this doesn't mean squat to me. When the application runs I can input the numbers fine, my "Clear" button works fine, but when I click the "calculate" button I get an error message saying that the application has crashed.

Below is my mainactivity.java file:

package edu.niagara.cis.coveringcostsbeta;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {

EditText m1, m2, m3, m4;
TextView sum1, sum2, sum3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    m1 = (EditText) findViewById(R.id.price);
    m2 = (EditText) findViewById(R.id.recipe);
    m3 = (EditText) findViewById(R.id.portion);
    m4 = (EditText) findViewById(R.id.costBudget);

    sum1 = (TextView) findViewById(R.id.costPortion);
    sum2 = (TextView) findViewById(R.id.idealPrice);
    sum3 = (TextView) findViewById(R.id.costActual);
}

public void goCalculate(View v) {
    String str_m1 = m1.getText().toString();
    String str_m2 = m2.getText().toString();
    String str_m3 = m3.getText().toString();
    String str_m4 = m4.getText().toString();
    if (str_m1.equals(""))
        str_m1 = "0";
    if (str_m2.equals(""))
        str_m2 = "0";
    if (str_m3.equals(""))
        str_m3 = "0";
    if (str_m4.equals(""))
        str_m4 = "0";

    int i_m1 = Integer.parseInt(str_m1);
    int i_m2 = Integer.parseInt(str_m2);
    int i_m3 = Integer.parseInt(str_m3);
    int i_m4 = Integer.parseInt(str_m4);

    long l_sum1 = (long) (i_m2 / i_m3);
    long l_sum2 = (long) ((i_m2 / i_m3) / i_m4);
    long l_sum3 = (long) (l_sum1 / i_m1);

    String str_sum1 = Long.toString(l_sum1);
    sum1.setText(str_sum1);
    String str_sum2 = Long.toString(l_sum2);
    sum2.setText(str_sum2);
    String str_sum3 = Long.toString(l_sum3);
    sum3.setText(str_sum3);
}

public void goClear(View v) {
    m1.setText("");
    m2.setText("");
    m3.setText("");
    m4.setText("");
    sum1.setText("Cost Per Portion");
    sum2.setText("Ideal Price");
    sum3.setText("Food Cost Actual");
}
}

and my mainactivity.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText 
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal"
        android:text="My Menu Price"
        android:ems="10" >
    </EditText>

    <EditText
        android:id="@+id/recipe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal"
        android:text="My Recipe Cost"
        android:ems="10" >
    </EditText>

    <EditText
        android:id="@+id/portion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal"
        android:text="# of Portions"
        android:ems="10">
    </EditText>

    <EditText
        android:id="@+id/costBudget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal"
        android:text="Food Cost Budget %"
        android:ems="10">
    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

     <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="goCalculate"
        android:text="Calculate" />
      <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="goClear"
        android:text="Clear" />

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> 
<TextView
        android:id="@+id/costPortion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cost Per Portion"
        android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
        android:id="@+id/idealPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ideal Price"
        android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
        android:id="@+id/costActual"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Food Cost Actual"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


</LinearLayout>

I don't think it has to do with the xml file but I wanted to provide as much information as possible. I'm almost positive that my calculations are causing the crash, but I don't know specifically how its causing this. Is it because in my calculations, one of the variables is another sum? :

long l_sum1 = (long) (i_m2 / i_m3);
long l_sum2 = (long) ((i_m2 / i_m3) / i_m4);
long l_sum3 = (long) (l_sum1 / i_m1);

The LogCat isn't much help either, the only message that pops up is: E -1 Device Disconnected: 1 I've also tried cleaning the program and running it again but that didn't change anything.

I'll gladly post any other information if it will help make more sense. I greatly appreciate any and all help/criticisms.

-Joe

Here is the actual ACTUAL LogCat, sorry about the mixup!

04-07 14:39:46.971: D/dalvikvm(1704): Not late-enabling CheckJNI (already on)
04-07 14:39:50.981: D/(1704): HostConnection::get() New Host Connection established 0xb8062d20, tid 1704
04-07 14:39:51.081: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:39:51.111: D/OpenGLRenderer(1704): Enabling debug mode 0
04-07 14:40:02.001: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:03.961: D/dalvikvm(1704): GC_FOR_ALLOC freed 146K, 7% free 3091K/3308K, paused 25ms, total 32ms
04-07 14:40:10.411: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:14.152: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:19.062: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:27.232: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:29.772: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:30.752: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:32.762: D/dalvikvm(1704): GC_FOR_ALLOC freed 421K, 14% free 3184K/3676K, paused 27ms, total 43ms
04-07 14:40:37.252: D/AndroidRuntime(1704): Shutting down VM
04-07 14:40:37.252: W/dalvikvm(1704): threadid=1: thread exiting with uncaught exception (group=0xb2cf2b20)
04-07 14:40:37.262: E/AndroidRuntime(1704): FATAL EXCEPTION: main
04-07 14:40:37.262: E/AndroidRuntime(1704): Process: edu.niagara.cis.coveringcostsbeta, PID: 1704
04-07 14:40:37.262: E/AndroidRuntime(1704): java.lang.IllegalStateException: Could not execute method of the activity
04-07 14:40:37.262: E/AndroidRuntime(1704):     at android.view.View$1.onClick(View.java:3823)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at android.view.View.performClick(View.java:4438)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at android.view.View$PerformClick.run(View.java:18422)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at android.os.Handler.handleCallback(Handler.java:733)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at android.os.Looper.loop(Looper.java:136)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at dalvik.system.NativeStart.main(Native Method)
04-07 14:40:37.262: E/AndroidRuntime(1704): Caused by: java.lang.reflect.InvocationTargetException
04-07 14:40:37.262: E/AndroidRuntime(1704):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at android.view.View$1.onClick(View.java:3818)
04-07 14:40:37.262: E/AndroidRuntime(1704):     ... 11 more
04-07 14:40:37.262: E/AndroidRuntime(1704): Caused by: java.lang.NumberFormatException: Invalid int: "9.00"
04-07 14:40:37.262: E/AndroidRuntime(1704):     at java.lang.Integer.invalidInt(Integer.java:137)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at java.lang.Integer.parse(Integer.java:374)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at java.lang.Integer.parseInt(Integer.java:365)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at java.lang.Integer.parseInt(Integer.java:331)
04-07 14:40:37.262: E/AndroidRuntime(1704):     at edu.niagara.cis.coveringcostsbeta.MainActivity.goCalculate(MainActivity.java:43)
04-07 14:40:37.262: E/AndroidRuntime(1704):     ... 14 more
Was it helpful?

Solution

According to this

java.lang.NumberFormatException: Invalid int: "9.00"

you are trying to cast a float to an integer at line 43

edu.niagara.cis.coveringcostsbeta.MainActivity.goCalculate(MainActivity.java:43)

You should set

android:inputType="number"

to your EditTexts.

OTHER TIPS

It seems the problem begins here:

04-07 14:40:37.262: E/AndroidRuntime(1704):     at edu.niagara.cis.coveringcostsbeta.MainActivity.goCalculate(MainActivity.java:43)

What line of code is this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top