I\'m programming Android for the first time and I\'m having some difficulties. The idea is to make a guessing game app in which the user takes a number in his/her head and the a
- where does my reasoning fail?
You are using the wrong setText()
method. In the TextView Docs you will see that there is one which takes an int
, this is for retrieving a String
resource that you have in your strings.xml
so you would pass it a resource id
. So your setText()
is looking for a resource
with the id
of whatever your answer
variable is. You will want to convert this to a String
with something like
buttonTextView.setText(String.valueof(answer));
or one of several different ways.
- How can I debug things like this?
When your app crashes there will be an exception in your logcat. This answer can help you to read your logcat. To open your logcat window in Eclipse, if it isn't already, you can do this
Window --> Show View --> Other --> Android --> LogCat
A couple side notes
You should change your params
like in onClick()
to something meaningful so I would change
public void onClick(View arg0)
to something like
public void onClick(View v) // v for view, could also be view, btn
// whatever makes sense to you and others who may read it
You also should compare the id
of your View
clicked instead of the View
itself. So you would compare it with something like the following (assuming you changed arg0
to v
)
if (v.getId() == R.id.start) // Assuming start is the id in your xml of your Button
// this will also allow you to use a switch statement
Your variables in onClick()
(min
, max
, and answer
) should be initialized outside of onClick()
or they will be reset to the default values with each click which I'm pretty sure you don't want (thanks to 323go for pointing that out).