Jake Piddington
Jake Piddington

Reputation: 11

My Android Compound Interest Calculator keeps crashing

package dum.de.dum;

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

normal imports?

public class CalcActivity extends Activity {


    Button finish;
    double total;
    double interest2;
    double time2;
    double base2;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {




        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        finish = (Button) findViewById(R.id.total);
        finish.setOnClickListener(new OnClickListener(){
            public void onClick(View V) {

                EditText base;
                base = (EditText) findViewById(R.id.base);
                String baseValue = base.getText().toString();
                double BaseNum = Double.parseDouble(baseValue);

                EditText interest;
                interest = (EditText) findViewById(R.id.interest);
                String interestValue = interest.getText().toString();
                double interestNum = Double.parseDouble(interestValue);

                EditText time;
                time = (EditText) findViewById(R.id.time);
                String timeValue = time.getText().toString();
                double timeNum = Double.parseDouble(timeValue);

                double total = BaseNum * Math.pow(interestNum + 1, timeNum);        

I've heard that people have had trouble when they parse numbers so I'm not sure if that could be a problem and if it is I have no idea how to fix it

        EditText Output;
        Output = (EditText) findViewById(R.id.total);
        Output.setText("Your total is: " + total);

        };

    });
}
}

every time I try to run it in the emulator I immediately get a force close. I'm a new-ish programmer and I don't get any error codes in eclipse.

Upvotes: 1

Views: 1428

Answers (4)

yash jain
yash jain

Reputation: 1

You should also have to show us the Xml layout file because you have first take the finish as a button and then you are assigning the button edit text. R.id.total is button in xml file or a edit text if its is a button have have to use the edittext box id over their in

Output = (EditText) findViewById(R.id.(edittext button id));

then the value will be display on the edit text box.

Upvotes: 0

jeet
jeet

Reputation: 29199

You can see error, by openeing view in eclipse by menu->window->showView->logcat,

As from your description, that application crashes as soon you launch the activity, the problematic area may be:

finish = (Button) findViewById(R.id.total);
        finish.setOnClickListener(new OnClickListener(){

check once in main.xml button id is given as-> android:id/total

Upvotes: 0

Squonk
Squonk

Reputation: 48871

finish = (Button) findViewById(R.id.total);

The id of your finish button isn't R.id.total - that's an EditText according to this...

Output = (EditText) findViewById(R.id.total);

You'll be getting a ClassCastException when trying to find the button using that id.

Upvotes: 3

FabianCook
FabianCook

Reputation: 20557

Try to replace

Output.setText("Your total is: " + total);

with:

Output.setText("Your total is: " + String.valueOf(total));

Upvotes: 2

Related Questions