"Build a Game with Sprite Kit" was retired on May 31, 2020.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Why is my app crashing again? Help appreciated!

This is the part that is crashing:

//What will happen
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        ImageButton advancedButton = (ImageButton) findViewById(R.id.advancedButton);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.advanced);
            }
        };

        advancedButton.setOnClickListener(listener);

        ImageButton imageButtonRip = (ImageButton) findViewById(R.id.imageButtonRip);
        View.OnClickListener mamans = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.activity_main);
            }
        };

        imageButtonRip.setOnClickListener(mamans);

    }

This is my logcat, the caused by part:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at everingapplications.blue_jay.MainActivity.onCreate(MainActivity.java:90)

Any ideas why Jon Kussmann and Gloria Dwomoh

1 Answer

You are asking your Activity to find R.id.imageButtonRip while the layout is still R.layout.activity_main. You didn't post your layout files, but I imagine that button only exists in the layout R.layout.advanced

Is there a reason why you aren't just switch activities instead of attempting to change the layout of one activity?

To be honest Jon Kussmann,

I really do not know. Bu it works! Thank you so so much! I do have a bug, I can only do it once, but that is okay!