0

I have a problem and I can't seem to set 2 onClickListener for 2 separate buttons located on 2 different layout, when running the program, it cause an exception to occur.

btnClickToSecondPage button is located in activity_main.xml layout and btnObjClickToGoToFirstPage button is located at second_activity.xml layout.

The java code for my program is located below here

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(getWindow().FEATURE_NO_TITLE);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    Button btnObjClickToGoToSecondPage = (Button) findViewById(R.id.btnClickToSecondPage);
    Button btnObjClickToGoToFirstPage = (Button) findViewById(R.id.btnChangetoFirstPage);


    btnObjClickToGoToFirstPage.setOnClickListener(
         new Button.OnClickListener(){
             @Override
             public void onClick (View v)
             {
                 setContentView(R.layout.second_activity);
             }
         }
    );
    btnObjClickToGoToSecondPage.setOnClickListener(
            new Button.OnClickListener(){
                @Override
                public void onClick (View v)
                {
                    setContentView(R.layout.activity_main);
                }
          }
    );
} }

Please help me rectified the problem thanks.

  • I think that problem with setContentView inside click listeners. You initialized buttons in the first layout. After first click layout will changed and buttons will not have valid context. Try to replace setContentViews with logs inside onclick listeners – Vigen Dec 25 '16 at 17:06

3 Answers3

0

Please implement the View.Onclick listener not Button.onclick listener

btnObjClickToGoToFirstPage.setOnClickListener(
         new View.OnClickListener(){
             @Override
             public void onClick (View v)
             {
                 setContentView(R.layout.second_activity);
             }
         }
    );
Mushirih
  • 451
  • 5
  • 13
  • Just to add that I just discovered that the program cause an exception when both of the buttons are located on different layouts. I have no exception with this code when both buttons are in the same layout. – Koh Yi Min Jason Dec 25 '16 at 17:45
  • I have edited the question as I just discovered that the program cause an exception when both of the buttons are located on different layouts. I have no exception with setting 2 onClickListener() when both the buttons are located in the same layout. – Koh Yi Min Jason Dec 25 '16 at 17:47
0

It is not a proper way switching pages in Android. Use two activities for switching pages with intents.

Intent newPage = new Intent (this, YourActivityNameForNewPage.class);
startActivity(newPage);

Put above code in your button's onClick().

Myo Ko
  • 108
  • 8
0

If you want to show a new page you either start a new activity or start a new fragment.

Changing the contentView is not the correct way to approach this and should not be done.

You refer to the documentation on activities here.

Assuming you have another Activity called SecondActivity here is how you would start it:

btnObjClickToGoToSecondPage.setOnClickListener(
        new Button.OnClickListener(){
            @Override
            public void onClick (View v)
            {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
      }
);

And then you define the layout in the XML of the new activity, that is second_activity.xml

If they all have similar layouts using a fragment is also a good option.

Basically you start a new activity or fragment to show anything new or change the data dynamically say on your button's onClick().

This question may further clear your doubts: What is setContentView(R.layout.main)?

Community
  • 1
  • 1
Avin
  • 81
  • 4