0

I am very new to java and android programming (apologies), and I am struggling with this coding!

I am attempting to make an app which will calculate a couple of different basic formulae:

((r*i)*s)/(m*1000)     (two instances of such)

((r_1*i_1)+(r_2*i_2))*s)/(m*1000)

I had many issues with getting the calculation to work so I began "playing" with the code.. and now I've done something bad with it and it crashes on start up :( wondering if anyone can see anything I've done wrong or overlooked?

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


public class MainActivity extends Activity {
        //declare fields
         EditText radius1;
         EditText radius2;
         EditText imbalance1;
         EditText imbalance2;
         EditText speed;
         EditText totalmass;
         TextView Gplane1;
         TextView Gplane2;
         TextView Gtotal;
         Button calc;


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

        //Bind the EditText,TextView and Button views

        radius1 = (EditText)findViewById(R.id.ra1i);
        radius2 = (EditText)findViewById(R.id.rad2i);
        imbalance1 = (EditText)findViewById(R.id.imb1i);
        imbalance2 = (EditText)findViewById(R.id.imb2i);
        speed = (EditText)findViewById(R.id.sp1);
        totalmass = (EditText)findViewById(R.id.mT1i);


        Gplane1 = (TextView)findViewById(R.id.G1o);
        Gplane2 = (TextView)findViewById(R.id.G2o);
        Gtotal = (TextView)findViewById(R.id.GTo);    

        calc = (Button)findViewById(R.id.calc1);
        calc.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v){
                //When the button is clicked, call the calculate method
                calculate();
            }
        });
    }

            public void calculate(){
            //get entered texts from edits, and convert to integers.
                Double r1 = Double.parseDouble(radius1.getText().toString());
                Double r2 = Double.parseDouble(radius2.getText().toString());
                Double im1 = Double.parseDouble(imbalance1.getText().toString());
                Double im2 = Double.parseDouble(imbalance2.getText().toString());
                Double sp = Double.parseDouble(speed.getText().toString());
                Double tm = Double.parseDouble(totalmass.getText().toString());
                //calculation
                Double Gpl1 = (r1*im1*sp)/(tm*1000);
                Double Gpl2 = (r2*im2*sp)/(tm*1000);
                Double Gt = ((r1*im1)+(r2*im2)*sp)/(tm*1000);
                //set view texts
                Gplane1.setText(Gpl1.toString());
                Gplane2.setText(Gpl2.toString());
                Gtotal.setText(Gt.toString());
            }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
        }

}

any help would be greatly appreciated!

Best Regards, Jake

kosa
  • 65,990
  • 13
  • 130
  • 167
  • What does LogCat show? – PM 77-1 Mar 05 '14 at 22:45
  • take a look at the logcat, here the reason for your app crash will be logged, see http://stackoverflow.com/questions/12769798/how-to-open-logcat-in-eclipse-for-android-debug – donfuxx Mar 05 '14 at 22:45
  • Without logcat, impossible to say, but it would crash if any of your edit boxes are empty. – Simon Mar 05 '14 at 23:31
  • I figured it out ;') I referenced "sp1" for my speed input when it should have been sp1i ( i made sp1 the title and sp1i the input, which was kind of stupid as they're so similar) – user3385748 Mar 05 '14 at 23:37
  • A perfect example of why variables should always have meaningful names! `(radius1 * imbalance1 * speed)` is more readable and more maintainable. – Simon Mar 05 '14 at 23:55
  • I'm looking for a way to add units on this line now however, as I said I'm still a newbie at java/android: Gtotal.setText(String.format("%.2f", Gt).toString()); (Note code changed slightly since original post).. the units are simply a capital G ) – user3385748 Mar 06 '14 at 00:01

0 Answers0