Reputation: 175
In the code below, the first TextView
symbol cannot be resolved, and the findById
method cannot be resolved. Can someone explain to me what the problem is and how I can fix it?
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String fact = "";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
Upvotes: 0
Views: 2868
Reputation: 28
Make sure you are importing all the packages you need. Also make sure all the dependancies are working. Lastly make sure that there is a text view in the XML for the activity. Hooe some of this is helpful.
Upvotes: 0
Reputation: 284
You need to give more background. Are you in a fragment, or an activity? How was the layout set? I'm guessing the answer is that you need to be calling findViewById() on the View of your layout - e.g. view.findViewById()
but it depends on how your layout was set.
If you're in an activity, calling findViewById() on the Activity object will only work if the current Activity layout is set by setContentView. If your layout was set a different way, then you need to get the View object of the layout and call findViewById()
on it. If you're in a fragment, and you're in onCreateView() then the view has been passed in for you and you just need to call view.findViewById()
Upvotes: 1
Reputation: 338
I am asuming that you want some text to set on text view on click of button try this
final TextView factLabel = (TextView)findViewById(R.id.factTextView);
Button showFactButton = (Button)findViewById(R.id.showFactButton);
showFactButton.setOnClickListener(new View.onClickListener()
{
@Override
public void onClick(View v) {
String fact = "Your Text Here";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
});
Upvotes: 0