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