• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps simple calc app is kicking my butt! :-)

I built and app to do a calculation when the button is pressed. However, it force closes when the button is pressed! I have tried adjusting and troubleshooting, but it is eluding me.
Hopefully someone can help!
I will post the code below.
 
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>


<TableRow>
<TextView android:text="A Feedflow:" />
<EditText android:id="@+id/aff" />
</TableRow>

<TableRow>
<TextView android:text="B Feedflow:" />
<EditText android:id="@+id/bff" />
</TableRow>

<TableRow>
<TextView android:text="A FeedTemp:" />
<EditText android:id="@+id/aft" />
</TableRow>

<TableRow>
<TextView android:text="B FeedTemp:" />
<EditText android:id="@+id/bft" />
</TableRow>

<TableRow>
<TextView android:text="A S/G pressure:" />
<EditText android:id="@+id/asp" />
</TableRow>

<TableRow>
<TextView android:text="B S/G pressure:" />
<EditText android:id="@+id/bsp" />
</TableRow>

<TableRow>
<TextView android:text="A S/G Blowdown:" />
<EditText android:id="@+id/abd" />
</TableRow>

<TableRow>
<TextView android:text="B S/G Blowdown:" />
<EditText android:id="@+id/bbd" />
</TableRow>

<TableRow>
<TextView android:text="A UFM:" />
<EditText android:id="@+id/aufm" />
</TableRow>

<TableRow>
<TextView android:text="B UFM:" />
<EditText android:id="@+id/bufm" />
</TableRow>

<TableRow>
<TextView android:text="Enter orifice constant; 1=9.72, 2=8.52, 3=7.14" />
<EditText android:id="@+id/orf" />
</TableRow>

<TableRow>
<TextView android:text="License MW:" />
<EditText android:id="@+id/lmw" />
</TableRow>


<Button android:id="@+id/calc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:onClick="doCalc"/>



<TextView
android:id="@+id/power"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""

/>




</TableLayout>
 
package com.mycompany.heatbalance;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;




class main extends Activity {
EditText aff;
EditText bff;
EditText asp;
EditText bsp;
EditText abd;
EditText bbd;
EditText aft;
EditText bft;
EditText orf;
EditText aufm;
EditText bufm;

Button calc;
TextView power;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
aff = (EditText) findViewById(R.id.aff);

bff= (EditText) findViewById(R.id.bff);
asp= (EditText) findViewById(R.id.asp);
bsp= (EditText) findViewById(R.id.bsp);
abd= (EditText) findViewById(R.id.abd);
bbd= (EditText) findViewById(R.id.bbd);
aft= (EditText) findViewById(R.id.aft);
bft= (EditText) findViewById(R.id.bft);
aufm= (EditText) findViewById(R.id.aufm);
bufm= (EditText) findViewById(R.id.bufm);
orf= (EditText) findViewById(R.id.orf);
calc = (Button) findViewById(R.id.calc);
power= (TextView) findViewById(R.id.power);
}

void doCalc (View view) {

double Aff = Double.parseDouble(aff.getText().toString());
double Bff = Double.parseDouble(bff.getText().toString());
double Asp = Double.parseDouble(asp.getText().toString());
double Bsp = Double.parseDouble(bsp.getText().toString());
double Aft = Double.parseDouble(aft.getText().toString());
double Bft = Double.parseDouble(bsp.getText().toString());
double Abd = Double.parseDouble(abd.getText().toString());
double Bbd = Double.parseDouble(bbd.getText().toString());
double Aufm = Double.parseDouble(aufm.getText().toString());
double Bufm= Double.parseDouble(bufm.getText().toString());
double Orf = Double.parseDouble(orf.getText().toString());

double asgoutput=(Aff*Aufm*(377-(0.007937*Asp)-(0.3235*Aft)))-(Abd*(0.3575-(0.000007937*Asp)));
double bsgoutput=(Bff*Bufm*(376.8-(0.007909*Bsp)-(0.3235*Bft)))-(Bbd*(0.3574-(0.000007909*Bsp)));
double asgloss=(Abd*((0.00005135*Asp)+0.1082));
double bsgloss=(Bbd*((0.00005135*Bsp)+0.1082));


double HBP=(asgoutput+bsgoutput+asgloss+bsgloss-Orf);

power.setText("thermal power " +HBP);


}
}
 
Just a cursory look, but it seems that any empty edit field will cause a crash as a null would get passed to parseDouble.

It's good practice to check for nulls on your variables as any findview could easily return null because of a simple typo.

It would also be a good to check your crash stack. If a null is the problem it should point you to the correct line of code where the null reference caused the crash. If not the stack will probably point you in the right direction.
 
Thanks! I tried it without variables as one of my troubleshooting. Didn't help. Not sure how to check my crash stack, because I have been using AIDE. I installed Eclipse, but it is having issues. I think I need to wipe it and try again.
Is my use of onClick correct, to call the new method? Is that format correct? This code does not generate errors in AIDE, but Eclipse is not liking the public void doCalc (View view)
Thanks!
 
It's a little hard to tell if the format is correct since the post has a smilie on that particular line (it looks ok and the compiler didn't hiccup). However, the doCalc method should be public. If it's not, the app won't find it and crash.

You should definitely get a proper development environment together so that you can use the debugging tools.
 
It's a little hard to tell if the format is correct since the post has a smilie on that particular line (it looks ok and the compiler didn't hiccup). However, the doCalc method should be public. If it's not, the app won't find it and crash.

You should definitely get a proper development environment together so that you can use the debugging tools.

a mis-scoped member wouldn't cause a crash, but it would cause a compiler error. If it compiles, then the scope is fine. I haven't looked at the code due to its ill format, but yea...

@OP, a couple things:

1) please put your code in either
Code:
 tags or [HIGH] tags
2) please provide a copy (either via copy/paste, or attaching a text file) containing your log cat output. 

These will help us better analyze your situation.
 
Galth is right, make the doCalc function public and it works providing you put numbers in all the boxes. If you don't you will get an exception parsing the Double.
To get the crash log from AIDE click "More->Show Log cat".
 
Back
Top Bottom