Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing data through Intent and receiving it
    primarykey
    data
    text
    <p>I am trying to pass data through my Intent into the next activity so I can receive it and give a timer values.</p> <pre><code>Button TimerButton = (Button)findViewById(R.id.TimerActivityButton); TimerButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { Intent timer = new Intent (BeefActivity.this,TimerActivity.class); timer.putExtra("beefType", 5000); timer.putExtra("beefThickness", 5000); timer.putExtra("grillTime", 15000); startActivity(timer); } }); </code></pre> <p>I have tried different methods of receiving the values, and I keep getting a force close or compiler errors. I know this is simple so can somebody please show me how to do this. Thank you in advance!</p> <hr> <pre><code>This is force closing </code></pre> <ul> <li>I tried passing in int length = beefType and it force closed</li> <li>I tried changing the -1 to 200, it still force closed</li> <li>I put back int length = 20000 where it worked originally</li> </ul> <p>Just having <code>int beefType = getIntent().getIntExtra("beefType", -1);</code> at the top of my class makes it force close. No compiler errors. I am stuck :-( Here is how my code looks</p> <hr> <pre><code>package com.android.project1; import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.view.View; import android.widget.Button; import android.widget.TextView; public class TimerActivity extends Activity { int beefType = getIntent().getIntExtra("beefType", 200); TextView timeDisplay; MyCount counter; int state = 0; int length = 20000; long startTime = 0; long currentTime = 0; long timeElapsed = 0; long timeRemaining = 0; long prevTimeRemaining = 0; Button control; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.timer); timeDisplay = (TextView) findViewById(R.id.timer); control = (Button) findViewById(R.id.control); counter = new MyCount(length, 100); } public String formatTime(long millis) { String output = "00:00:00"; long seconds = millis / 1000; long minutes = seconds / 60; long hours = minutes / 60; seconds = seconds % 60; minutes = minutes % 60; hours = hours % 60; String secondsD = String.valueOf(seconds); String minutesD = String.valueOf(minutes); String hoursD = String.valueOf(hours); if (seconds &lt; 10) secondsD = "0" + seconds; if (minutes &lt; 10) minutesD = "0" + minutes; if (hours &lt; 10) hoursD = "0" + hours; output = hoursD + " : " + minutesD + " : " + secondsD; return output; } public void control(View view) { switch (state) { case 0: startTime = System.currentTimeMillis(); counter.start(); control.setText(R.string.pause); state = 1; break; case 1: // Pause currentTime = System.currentTimeMillis(); timeElapsed = currentTime - startTime; if (prevTimeRemaining == 0) timeRemaining = length - timeElapsed; else timeRemaining = prevTimeRemaining - timeElapsed; counter.cancel(); timeDisplay.setText("Left: " + String.valueOf(formatTime(timeRemaining))); control.setText(R.string.resume); prevTimeRemaining = timeRemaining; // Resume counter = new MyCount(timeRemaining, 100); state = 0; break; case 2: prevTimeRemaining = 0; counter = new MyCount(length, 100); control.setText(R.string.start); timeDisplay.setText(R.string.timer); state = 0; } } public class MyCount extends CountDownTimer { public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } public void onTick(long timeRemaining) { timeDisplay.setText("Left: " + formatTime(timeRemaining)); } public void onFinish() { timeDisplay.setText("Finished!"); state = 2; control.setText(R.string.restart); } } } </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.
 

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