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
Farouk Charkas
1,957 PointsI 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.
Farouk Charkas
1,957 PointsFor listener it says: Unknown class 'listener'
For setOnClickListener it says: Cannot resolve symbol 'setOnClickListener'
Farouk Charkas
1,957 PointsFor listener it says: Unknown class 'listener'
For setOnClickListener it says: Cannot resolve symbol 'setOnClickListener'
aakarshrestha
6,509 PointsMake 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!
Farouk Charkas
1,957 PointsI 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! :-)
Farouk Charkas
1,957 PointsI 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
Jon Kussmann
Courses Plus Student 7,254 PointsHi 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.
Farouk Charkas
1,957 PointsStill, it is still not working.
Farouk Charkas
1,957 Pointspackage 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);
}
Farouk Charkas
1,957 Pointspackage 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);
}
Jon Kussmann
Courses Plus Student 7,254 PointsIt looks like Aakar might have been onto something.
You declare your button/listener directly inside of the class instead of inside the onCreate method.
Farouk Charkas
1,957 PointsSo then what will I have to do?
Farouk Charkas
1,957 PointsSo then what will I have to do?
Jon Kussmann
Courses Plus Student 7,254 Pointspackage 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);
}
}
Farouk Charkas
1,957 PointsSorry for my lack of skills and my ambush of questions but, I get another error stating "Cannot resolve symbol 'savedInstanceState'"
Farouk Charkas
1,957 PointsSorry for my lack of skills and my ambush of questions but, I get another error stating "Cannot resolve symbol 'savedInstanceState'"
Jon Kussmann
Courses Plus Student 7,254 PointsTypo on my part.
Change:
protected void onCreate(Bundle savedInstance) {
To:
protected void onCreate(Bundle savedInstanceState) {
Farouk Charkas
1,957 PointsNow it cannot resolve symbol 'R' I have tried cleaning it then rebuilding it
Farouk Charkas
1,957 PointsNow it cannot resolve symbol 'R' I have tried cleaning it then rebuilding it
Jon Kussmann
Courses Plus Student 7,254 PointsHave you tried closing and opening Android Studio again?
Farouk Charkas
1,957 PointsNo I hve not tried that
Farouk Charkas
1,957 PointsNo I hve not tried that
miguelcastro2
Courses Plus Student 6,573 Pointsmiguelcastro2
Courses Plus Student 6,573 PointsWhat is the error you see when you hover over the red line?