通过意图传递数据,并接收其 [英] Passing data through Intent and receiving it

查看:112
本文介绍了通过意图传递数据,并接收其的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的意图进入下一个活动来传递数据,所以我可以接受它,并给定时器的值。

 按钮TimerButton =(按钮)findViewById(R.id.TimerActivityButton);
TimerButton.setOnClickListener(新View.OnClickListener(){
     公共无效的onClick(视图v)
     {
          意图定时器=新的意图(BeefActivity.this,TimerActivity.class);
          timer.putExtra(beefType,5000);
          timer.putExtra(beefThickness,5000);
          timer.putExtra(grillTime,15000);
          startActivity(定时器);
      }
});
 

我试图接受的价值观不同的方法,我不断收到一个强制关闭或编译器错误。我知道这是简单,所以可以有人请告诉我如何做到这一点。预先感谢您!


 这是力闭幕
 

  • 在我试图传递INT长度= beefType并强制关闭
  • 在我试图改变-1至200,但它仍然强制关闭
  • 在我放回INT长度= 20000,其中它的工作原来

只要有 INT beefType = getIntent()getIntExtra(beefType,-1); 在我班上名列前茅使得强制关闭。没有编译器错误。我坚持:-(下面是我的code相


 包com.android.project1;

进口android.app.Activity;
进口android.os.Bundle;
进口android.os.CountDownTimer;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;

公共类TimerActivity延伸活动{

    INT beefType = getIntent()getIntExtra(beefType,200)。

  TextView的timeDisplay;
  mycount的计数器;
  INT状态= 0;
  INT长度= 20000;
  长的startTime = 0;
  长currentTime的= 0;
  长timeElapsed = 0;
  长timeRemaining = 0;
  长prevTimeRemaining = 0;
  按钮控制;

  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.timer);

    timeDisplay =(TextView中)findViewById(R.id.timer);
    控制=(按钮)findViewById(R.id.control);
    计数器=新mycount的(长度,100);
  }
  公共字符串formatTime(long millis)来
  {
      字符串输出=00:00:00;
      长秒=米利斯/ 1000;
      长分=秒/ 60;
      长时间=分钟/ 60;

      秒=秒%60;
      分钟=分钟%60;
      小时=小时%60;

      字符串secondsD =将String.valueOf(秒);
      字符串minutesD =将String.valueOf(分钟);
      字符串hoursD =将String.valueOf(小时);

      如果(秒-1,10)
        secondsD =0+秒;
      如果(分钟小于10)
        minutesD =0+分钟;
      如果(小时小于10)
        hoursD =0+小时;

      输出= hoursD +:+ minutesD +:+ secondsD;
      返回输出;
    }
  公共无效控制(查看视图){
    开关(州){
    情况下0:
      的startTime = System.currentTimeMillis的();
      counter.start();
      control.setText(R.string.pause);
      状态= 1;
      打破;
    情况1:
      // 暂停
      currentTime的= System.currentTimeMillis的();
      timeElapsed = currentTime的 - 的startTime;
      如果(prevTimeRemaining == 0)
        timeRemaining =长度 -  timeElapsed;
      其他
        timeRemaining = prevTimeRemaining  -  timeElapsed;
      counter.cancel();
      timeDisplay.setText(左+将String.valueOf(formatTime(timeRemaining)));
      control.setText(R.string.resume);
      prevTimeRemaining = timeRemaining;

      // 恢复
      计数器=新mycount的(timeRemaining,100);
      状态= 0;
      打破;
    案例2:
      prevTimeRemaining = 0;
      计数器=新mycount的(长度,100);
      control.setText(R.string.start);
      timeDisplay.setText(R.string.timer);
      状态= 0;
    }
  }

  公共类mycount的扩展CountDownTimer
  {

    公共mycount的(长millisInFuture,长countDownInterval)
    {
      超(mil​​lisInFuture,countDownInterval);
    }
    公共无效onTick(长timeRemaining)
    {
        timeDisplay.setText(左+ formatTime(timeRemaining));
    }
    公共无效onFinish()
    {
      timeDisplay.setText(完了!);
      状态= 2;
      control.setText(R.string.restart);
    }
  }
}
 

解决方案

  INT beefType = getIntent()getIntExtra(beefType,-1)。
 

I am trying to pass data through my Intent into the next activity so I can receive it and give a timer values.

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

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!


This is force closing

  • I tried passing in int length = beefType and it force closed
  • I tried changing the -1 to 200, it still force closed
  • I put back int length = 20000 where it worked originally

Just having int beefType = getIntent().getIntExtra("beefType", -1); at the top of my class makes it force close. No compiler errors. I am stuck :-( Here is how my code looks


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

这篇关于通过意图传递数据,并接收其的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆