通过Intent传递数据并接收它

时间:2010-11-29 21:21:53

标签: android android-activity android-intent

我正在尝试通过我的Intent将数据传递到下一个活动中,这样我就可以接收它并给出一个计时器值。

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);
      }
});

我尝试了不同的接收值的方法,并且不断收到强制关闭或编译错误。我知道这很简单所以有人可以告诉我如何做到这一点。提前谢谢!


This is force closing
  • 我尝试传入int length = beefType并强行关闭
  • 我尝试将-1更改为200,仍然强行关闭
  • 我把int length = 20000放在最初工作的地方

只需将int beefType = getIntent().getIntExtra("beefType", -1);放在班级的顶部,就可以强行关闭。没有编译器错误。我被困了:-(这是我的代码看起来如何


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 < 10)
        secondsD = "0" + seconds;
      if (minutes < 10)
        minutesD = "0" + minutes;
      if (hours < 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);
    }
  }
}

2 个答案:

答案 0 :(得分:6)

int beefType = getIntent().getIntExtra("beefType", -1);

答案 1 :(得分:1)

你需要把这一行:

int beefType = getIntent().getIntExtra("beefType", 200);

在onCreate方法中,而不是作为字段初始值设定项。

下次 - 在询问之前阅读堆栈跟踪。如果您在阅读后仍然卡住,请将其附加到您的问题上。

祝你好运!