0

I have 2 layouts named "main.xml" and "main2.xml"

In main.xml, there's a button "new"

In main2.xml, there's a button "back"

button new:

Button button1 = (Button)findViewById(R.id.newstore);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.main2);

            }
        });

I change view to main2.xml successfully,

but when I add these code:

button back:

Button back = (Button)findViewById(R.id.back);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setContentView(R.layout.main);

        }
    });

I can't run this project anymore!! I have to forcekill it everytime!

What's the problem?

I can upload my project if you need it to answer.

mac
  • 42,153
  • 26
  • 121
  • 131
范植勝
  • 787
  • 2
  • 6
  • 14

1 Answers1

2

Calling setContentView multiple times is a very bad practice. When you want to use multiple layouts, use a viewflipper where each layout xml is a child to the viewflipper. In that case you can swtich between views using

viewflipper.setDisplayedChild(index of child);
blessanm86
  • 31,439
  • 14
  • 68
  • 79