-4

I have a page that has two buttons to "two" other pages, the thing is, button 1 and button 2 lead to a second page, thought button 1 should lead to the first page and button 2 should lead to the second page, here is the java file, I don't know how to put it!

ActiveMain.java

package com.d.di;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    Button button1;
    Button button2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
    }

    public void addListenerOnButton() {
        final Context context = this;
        button1 = (Button) findViewById(R.id.abus);
        button2 = (Button) findViewById(R.id.weoff);


         button1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    Intent intent = new Intent(context, PageOne.class);
                    startActivity(intent);

                }

            });

            button2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {

                    Intent intent = new Intent(context, PageTwo.class);
                    // you made a mistake here you called the PageOne again here while you should call the second. 

                    startActivity(intent);

                }

            });



    }

}

PageOne.java

package com.d.di;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class PageOne extends Activity {

    Button button1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.abus);
    }
}

PageTwo

package com.d.di;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class PageTwo extends Activity {

    Button button2;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weoff);
    }
}
Maza
  • 1
  • 3
  • For the OnClickListener for button2 you should use Intent intent = new Intent(context, PageTwo.class) to take you to a different activity. I'm not sure whether I understood your question correctly though. – epsilondelta Jun 29 '14 at 23:19
  • Is the code in the listeners the same? – Mar Johnson Jun 29 '14 at 23:20
  • 1
    Both of your buttons are opening same page: `PageOne.class` that what happens when copy paste and not fixing variables after that :) – Hardy Jun 29 '14 at 23:21
  • A fairly cursory examination of the code would have revealed your problem in short order. When you're tempted to post to SO, it might make sense to step away from the code and return to it before you post your question -- chances are, taking a break makes problems much more clear. – MarsAtomic Jun 29 '14 at 23:34
  • @epsilondelta I made another java file and named it PageTwo as you said, the first button leads to page 1 .. second button stuck!! – Maza Jun 29 '14 at 23:39
  • @Mar Johnson yes it is – Maza Jun 29 '14 at 23:40
  • @Marza what do you mean by stuck? – epsilondelta Jun 29 '14 at 23:41
  • It holds in mobile for counted seconds and a msg pops up "Unfortunately, Di has stopped" – Maza Jun 29 '14 at 23:44
  • It looks like you lack very basic knowledge of how android activities work. A simple google search gives hundreds of useful links you could use to answer your own question. http://developer.android.com/training/basics/firstapp/starting-activity.html read through this and try to figure out the answer for yourself. – Nic Robertson Jun 29 '14 at 23:48
  • possible duplicate of [How to start new activity on button click](http://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – Nic Robertson Jun 29 '14 at 23:49

1 Answers1

0
 button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, PageOne.class);
            startActivity(intent);

        }

    });

    button2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, PageTWO.class); // you made a  
 mistake here you called the PageOne again here while you should call the second. 

            startActivity(intent);

        }

    });
Kosh
  • 6,140
  • 3
  • 36
  • 67