-6

Possible Duplicate:
Generating random number in a range with Java
How can I generate random number in specific range in Android?

Here is my scenario From the Mainactivity ; On click of a Button i want to generate a random number from 1 to 4 Based on the output, i want to write an if-else that will call 4 different activities

So on click, if 4 is generated, then call activity 4 Next time 1 can be generated and should call activity 1 and so on ....

Can someone please help me with this code?

Community
  • 1
  • 1
Jasma
  • 133
  • 3
  • 12
  • 2
    "Can someone please help me with this code?" What code? Are you expecting someone to write the code for you? Please read the FAQs. You are expected to show some effort/research in attempting to solve your problem and then ask for specific help. – Simon Sep 26 '12 at 09:56

2 Answers2

0
   public void test1(){
    Random r = new Random();
    int index = r.nextInt(4)+1;

    Intent intent = null;

    switch(index){
    case 1:  intent = new Intent(this, Activity1.class);
        break;
    case 2:  intent = new Intent(this, Activity2.class);
        break;
    case 3:  intent = new Intent(this, Activity3.class);
        break;
    case 4:  intent = new Intent(this, Activity4.class);
        break;
    default:
     Log.e("ERROR", "");
            return;
    }
    if(intent != null){
       this.startActivity(intent);
    }
   }
ALiGOTec
  • 347
  • 2
  • 15
  • This is very much the code that i have got, but i always end up getting the first activity ALWAYS. – Jasma Sep 26 '12 at 10:12
  • final Intent intent = null; - i am getting error in this line. I have tried just final Intent intent and also with the = null. Getting null pointer exception – Jasma Sep 26 '12 at 10:18
  • it was just a quick start code, edited. – ALiGOTec Sep 26 '12 at 10:31
0
myBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Random r = new Random();
            int index = r.nextInt(4)+1;

            Intent intent;

            if (index == 4) {
                intent = new Intent(Activity.this, Activity4.class); 
            }
            else if (index == 3) {
                intent = new Intent(Activity.this, Activity3.class); 
            }
            else if (index == 2) {
            intent = new Intent(Activity.this, Activity2.class); 
            }
            else 
            intent = new Intent(Activity.this, Activity1.class);                    

            startActivity(intent);                
         }
    });
ThePCWizard
  • 3,338
  • 2
  • 21
  • 32
  • final Intent intent = null; - i am getting error in this line. I have tried just final Intent intent and also with the = null. Getting null pointer exception – Jasma Sep 26 '12 at 10:19
  • public void onClick(View v) { Random r = new Random(); int index = r.nextInt(4)+1; final Intent myintent1 = null; switch(index){ case 1: new Intent(LearningTimeMenu.this,colorActivity.class); break; case 2: new Intent(LearningTimeMenu.this,shapeActivity.class); break; case 3: new Intent(LearningTimeMenu.this,numberActivity.class); break; case 4: new Intent(LearningTimeMenu.this,colorActivity.class); return; } startActivity(myintent1); – Jasma Sep 26 '12 at 10:20
  • Where are you commenting? That's not my code... try my answer, then ask for help – ThePCWizard Sep 26 '12 at 10:23
  • if you are done, then please close this question, by accepting an answer... – ThePCWizard Sep 26 '12 at 10:35