【发布时间】:2013-12-01 08:42:55
【问题描述】:
我有 5 个不同的活动,每个活动都应该有一个随机数生成器,以便用户进行随机活动。
示例:
第 1 幕 ---> 幕。 3
第 3 幕 ---> 幕。 4
第 2 幕 ---> 幕。 5
注意:因此,从 Activity 5 开始,用户应该无法返回到之前的 Activity。
所以这是我在每个活动中写的去随机活动,我做了一些变量,所以用户去的下一个活动,下一个活动将知道用户之前访问过的以前的活动。 这是用户将开始的第一个活动。
public void on_quiz_1_wrong(View v){
score = score - 2;
String visited = "1";
txtScore.setText(String.valueOf(score) );
int r1 = r.nextInt(4)+1;
if(r1 == 1){
Intent q1 = new Intent(this,Quiz_2.class);
Bundle bundle1 = new Bundle();
bundle1.putString("passVisit", visited);
q1.putExtras(bundle1);
q1.putExtra("passScore", score);
startActivity(q1);
}
else if(r1 == 2){
Intent q1 = new Intent(this,Quiz_3.class);
Bundle bundle1 = new Bundle();
bundle1.putString("passVisit", visited);
q1.putExtras(bundle1);
q1.putExtra("passScore", score);
startActivity(q1);
}
else if(r1 == 3){
Intent q1 = new Intent(this,Quiz_4.class);
Bundle bundle1 = new Bundle();
bundle1.putString("passVisit", visited); // visited = 1 ...> string value
q1.putExtras(bundle1);
q1.putExtra("passScore", score);
startActivity(q1);
}
else if(r1 == 4){
Intent q1 = new Intent(this,Quiz_5.class);
Bundle bundle1 = new Bundle();
bundle1.putString("passVisit", visited);
q1.putExtras(bundle1);
q1.putExtra("passScore", score);
startActivity(q1);
}
而且,那是另一段代码,我把每个活动都放进去,所以现在活动知道上一次访问,所以随机数生成器不应该生成用户已经访问过的数字。
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz_3);
txtScore = (TextView) findViewById(R.id.q_result); // declaring the TextView
correctB = (Button) findViewById(R.id.quiz_3_correct);
wrongB = (Button) findViewById(R.id.quiz_3_wrong);
r = new Random();
// Getting previous score
Bundle extras = getIntent().getExtras();
if (extras != null)
{
score = extras.getInt("passScore");
}
txtScore.setText(String.valueOf(score));
Bundle getBundle = getIntent().getExtras();
last_visit = getBundle.getString("passVisit"); // If previous was quiz 1 the---> last_visit is = 1
} // end of onCreate
如果用户想从前一个活动中获得随机活动:
public void on_quiz_3_wrong(View v){
score = score - 2;
String visited = "3";
txtScore.setText(String.valueOf(score) );
int r1 = r.nextInt(4)+1;
if(r1 == 2 && !last_visit.equals("2") ){
Intent q1 = new Intent(this,Quiz_2.class);
Bundle bundle1 = new Bundle();
bundle1.putString("passVisit", visited);
q1.putExtras(bundle1);
q1.putExtra("passScore", score);
startActivity(q1);
}
if(r1 == 4 && !last_visit.equals("4") ){
Intent q1 = new Intent(this,Quiz_4.class);
Bundle bundle1 = new Bundle();
bundle1.putString("passVisit", visited);
q1.putExtras(bundle1);
q1.putExtra("passScore", score);
startActivity(q1);
}
if(r1 == 5 && !last_visit.equals("5") ){
Intent q1 = new Intent(this,Quiz_5.class);
Bundle bundle1 = new Bundle();
bundle1.putString("passVisit", visited);
q1.putExtras(bundle1);
q1.putExtra("passScore", score);
startActivity(q1);
}
}
我知道这很长,很抱歉,但我想尽可能详细地告诉你,因为我知道这有点令人困惑。
我从 9 月就开始尝试让这个工作,但仍然没有运气,我会很高兴的。 谢谢!!!
【问题讨论】:
-
您的问题到底是什么?应用程序是否崩溃,如果是,请发布堆栈跟踪。什么不能正常工作?
-
你必须告诉我们真正的问题是什么,我不能轻易地浏览所有未格式化的代码。什么不工作?
-
首先,应用程序崩溃,即使崩溃消失,随机系统也不起作用!
-
@user2547460 在进行任何其他活动之前尝试完成每个活动。
-
@GrIsHu 正确回答了这个问题:finish() 杀死当前活动,如果你不调用 finish() 用户可以通过按返回键返回它。有关这些类型的更多信息,请访问:developer.android.com/reference/android/app/Activity.html
标签: java android debugging random android-activity