Note that there are some explanatory texts on larger screens.

plurals
  1. POerror trying to change activities using onClick()
    primarykey
    data
    text
    <p>I have the following program that does a calculation based on user input.But when I click the <code>calc</code> button which is referenced in <code>onClick()</code> nothing happens.As in it doesn't navigate to the result screen.Can somebody shed some light on this as I'm not sure why iit isn't changing intent.</p> <p>My main class is like this:</p> <pre><code>public class MainActivity extends Activity implements android.view.View.OnClickListener { //variables for xml objects EditText offsetLength,offsetDepth,ductDepth; Button calculate; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //setting the variables to the xml id's and setting the click listener on the calc button offsetLength = (EditText)findViewById(R.id.offLength); offsetDepth = (EditText)findViewById(R.id.offDepth); ductDepth = (EditText)findViewById(R.id.ductDepth); calculate = (Button)findViewById(R.id.calc); calculate.setOnClickListener((OnClickListener) this); } //called when button is clicked. public void OnClick(View v) { try { String getoffsetlength = offsetLength.getText().toString(); String getoffsetdepth = offsetDepth.getText().toString(); String getductdepth = ductDepth.getText().toString(); double tri1,tri2; double marking1,marking2; double off1 = Double.parseDouble(getoffsetlength); double off2 = Double.parseDouble(getoffsetdepth); double off3 = Double.parseDouble(getductdepth) ; marking1 = Math.pow(off1,2) + Math.pow(off2,2); tri1 = (float)off2/(float)off1; tri2 = (float)off3/Math.atan((float)tri1); marking2 = (float)off3/Math.atan(tri2); Intent myIntent = new Intent(this, CalcResult.class); myIntent.putExtra("number1", marking1); myIntent.putExtra("number2", marking2); startActivity(myIntent); Intent i = new Intent(this,CalcResult.class); } catch (NumberFormatException e) { // TODO: handle exception System.out.println("Must enter a numeric value!"); } } </code></pre> <p>Also this is the class where my calculation that should be navigated to:</p> <pre><code> public class CalcResult extends MainActivity { EditText result1,result2; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.result); result1 = (EditText)findViewById(R.id.mark1); result2 = (EditText)findViewById(R.id.mark2); Intent intent = getIntent(); Bundle bundle = intent.getExtras(); double mark1 = bundle.getDouble("number1"); double mark2 = bundle.getDouble("number2"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload