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

I need this error fixed! Any feedback is appreciated!

This is my code:

    Button catchesButton = (Button) findViewById(R.id.catchesButton);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setContentView(R.layout.catchpage);

        }
    };
    catchesButton.setOnClickListener(listener);

I just cant seem to fix it- It has setOnClickListener underlined in red and so is listener.

What is the error you see when you hover over the red line?

For listener it says: Unknown class 'listener'

For setOnClickListener it says: Cannot resolve symbol 'setOnClickListener'

For listener it says: Unknown class 'listener'

For setOnClickListener it says: Cannot resolve symbol 'setOnClickListener'

Make sure you are writing everything inside onCreate() method.

public class MainActivity extends Activity {

private Button catchesButton;

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

    catchesButton = (Button) findViewById(R.id.catchesButton);
    View.onClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  setContentView(R.layout.catchpage);

            }
    };
     catchesButton.setOnClickListener(listener);
}

}

Happy coding!

I greatly appreciate your suggestion AAKAR SHRESTHA,

I really liked your comment "Happy Coding!" but your suggestion did not work for me, I might have done something wrong but still thank you, but it did not work out for me! :-)

I greatly appreciate your suggestion AAKAR SHRESTHA,

I really liked your comment "Happy Coding!" but your suggestion did not work for me, I might have done something wrong but still thank you, but it did not work out for me! :-)

6 Answers

Hi Evering,

Your code does look fine to me. Could you try going to Build -> Clean Project and then Build -> Rebuild Project?

Also check to see if you have the proper imports. If you could post the entire file, that would be useful as well.

Still, it is still not working.

package everingapplications.fishinglife;

import android.app.Activity; import android.view.View; import android.widget.Button;

/**

  • Created by Doctors on 8/5/2015. */ public class Catch extends Activity {

    Button catchesButton = (Button) findViewById(R.id.catchesButton); View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { setContentView(R.layout.catchpage);

    }
    

    }; catchesButton.setOnClickListener(listener);

}

package everingapplications.fishinglife;

import android.app.Activity; import android.view.View; import android.widget.Button;

/**

  • Created by Doctors on 8/5/2015. */ public class Catch extends Activity {

    Button catchesButton = (Button) findViewById(R.id.catchesButton); View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { setContentView(R.layout.catchpage);

    }
    

    }; catchesButton.setOnClickListener(listener);

}

It looks like Aakar might have been onto something.

You declare your button/listener directly inside of the class instead of inside the onCreate method.

So then what will I have to do?

So then what will I have to do?

package everingapplications.fishinglife;

import android.app.Activity; import android.view.View; import android.widget.Button;

/** * Created by Doctors on 8/5/2015. */ public class Catch extends Activity {

@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout); // you will need to use the name of your layout here
Button catchesButton = (Button) findViewById(R.id.catchesButton);
View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        setContentView(R.layout.catchpage);

    }
};
catchesButton.setOnClickListener(listener);
}
}

Sorry for my lack of skills and my ambush of questions but, I get another error stating "Cannot resolve symbol 'savedInstanceState'"

Sorry for my lack of skills and my ambush of questions but, I get another error stating "Cannot resolve symbol 'savedInstanceState'"

Typo on my part.

Change:

protected void onCreate(Bundle savedInstance) {

To:

protected void onCreate(Bundle savedInstanceState) {

Now it cannot resolve symbol 'R' I have tried cleaning it then rebuilding it

Now it cannot resolve symbol 'R' I have tried cleaning it then rebuilding it

Have you tried closing and opening Android Studio again?

No I hve not tried that

No I hve not tried that