Efaz
Efaz

Reputation: 304

Android switching to multiple layouts

This is my code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.get_started1);

        mNextBtn1 = findViewById(R.id.getStartedBtn);

        mNextBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.get_started2);
                mNextBtn2 = findViewById(R.id.button2);
            }
        });

        mNextBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.get_started3);
            }
        });


    }

I am getting this error:

Process: com.example.storefrontapp, PID: 32160
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.storefrontapp/com.example.storefrontapp.Get_Started}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)

What I am trying to achieve is to change layouts when button click. I want to show 1 field at a time as the user clicks next? For example, first user sees only Name TextField and click next to see Address TextField etc..Kinda like a registration page but with 1 field at a time.

I am creating 1 activity that points to multiple layouts. I can have Layout1, Layout2 Layout3 etc. Next button on Layout1 takes user to Layout2 and so on.?

Upvotes: 0

Views: 37

Answers (2)

Ben P.
Ben P.

Reputation: 54194

Move your mNextBtn2.setOnClickListener() call into the click listener for mNextBtn1:

mNextBtn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        setContentView(R.layout.get_started2);
        mNextBtn2 = findViewById(R.id.button2);

        mNextBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.get_started3);
            }
        });
    }
});

The way you have it now, your app will still try to set the click listener for mNextBtn2 immediately. Since the button is null until the user taps mNextBtn1, this will throw an exception.


If you're concerned about this nesting poorly, consider something like this instead. In your layout files, add the following attribute to each button you'd normally call setOnClickListener() for:

android:onClick="switchLayout"

Then, in your activity, create the switchLayout method:

public void switchLayout(View view) {
    switch (view.getId()) {
        case R.id.getStartedBtn:
            setContentView(R.layout.get_started2)
            break;

        case R.id.button2:
            setContentView(R.layout.get_started3)
            break;

        ...
    }
}

Now you don't need to call setOnClickListener() at all; your buttons will just automatically call through to this switchLayout() method when you click them. And you can use the button's id to figure out which layout to swap in.

Upvotes: 1

Yunus Kulyyev
Yunus Kulyyev

Reputation: 1022

You are getting that error because you did not initialize your second button. You can't attach a listener before initializing.

You have two options. Either initialize your second button outside of the listener like this:

mNextBtn1 = findViewById(R.id.getStartedBtn);
mNextBtn2 = findViewById(R.id.button2);

or put your second button inside of the listener of the first button:

mNextBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.get_started2);
                mNextBtn2 = findViewById(R.id.button2);
mNextBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.get_started3);
            }
        });
            }
        });
   

Upvotes: 0

Related Questions