【发布时间】:2014-04-08 10:53:41
【问题描述】:
我之前尝试过通过 Intent 传递变量。但似乎我错过了一些我无法通过整数变量时间的东西。其他人之前已经问过这个问题,但我无法在这种情况下使用它。
我想通过intent将整型变量time的值传递给aftertap.class的另一个类。
我有这个代码。
public class ingame extends Activity {
private TextView textTimer;
int time = 0;
Timer t;
TimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ingame);
Button button = (Button)findViewById(R.id.my_button);
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
params.leftMargin = button.getWidth()+ new Random().nextInt(displaymetrics.widthPixels - 2*button.getWidth());
params.topMargin = button.getHeight()+ new Random().nextInt(displaymetrics.heightPixels - 3*button.getHeight());
Log.v("widthPixels", ""+ displaymetrics.widthPixels);
Log.v("heightPixels", ""+ displaymetrics.heightPixels);
button.setLayoutParams(params);
startTimer();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
t.cancel();
t.purge();
//Starting a new Intent
Intent thirdscreen = new Intent(getApplicationContext(), aftertap.class);
//Sending data to another Activity
thirdscreen.putExtra("time", time);
startActivity(thirdscreen);
}
});
}
public void startTimer(){
t = new Timer();
task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
textTimer = (TextView) findViewById(R.id.textTimer);
textTimer.setText(time + "");
time = time + 1;
}
});
}
};
t.scheduleAtFixedRate(task, 0, 1);
}
}
Aftertap 类:
public class aftertap extends Activity{
TextView score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aftertap);
TextView gameover = (TextView)findViewById(R.id.gameover);
score = (TextView)findViewById(R.id.score);
Intent i = getIntent();
// Receiving the Data
//I do not know how to get the value of the integer variable time. I want to display the value of time in the TextView score
}
}
【问题讨论】:
-
你遇到了什么错误?
标签: android android-intent integer