当前位置 : 主页 > 手机开发 > android >

android – 通过Intent传递数据并接收它

来源:互联网 收集:自由互联 发布时间:2021-06-11
我试图通过我的Intent将数据传递到下一个活动,这样我就可以收到它并给出一个计时器值. Button TimerButton = (Button)findViewById(R.id.TimerActivityButton);TimerButton.setOnClickListener(new View.OnClickListener
我试图通过我的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);
    }
  }
}
int beefType = getIntent().getIntExtra("beefType", -1);
网友评论