Looks like you are calling setText(int resid) whereas what you really
want is setText(CharSequence text):
http://developer.android.com/reference/android/widget/TextView.html#setText(int)
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

Convert your integer to a String before calling to fix it. Use
Integer.toString(int i) or concatenate it to an empty String, for
example.

Also, random.nextInt(9) returns 0 to 8, both ends inclusive.

On Jan 28, 4:14 pm, Tag <[email protected]> wrote:
> I'm very new to android and java, so I don't have a very good
> understanding of how to work through certain problems other than
> taking wild stabs here and there.
>
> The problem i'm trying to overcome is generating a simple random
> number between 1 and 9 from the click of a button and getting that
> number to appear in a TextView.
>
> This is what I have so far, but it always fails at the .setText()
>
> public class AndroidRandom extends Activity {
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>         Button b = (Button)findViewById(R.id.Button);
>
>         b.setOnClickListener(new View.OnClickListener() {
>                         public void onClick(View v) {
>                                 TextView a = 
> (TextView)findViewById(R.id.Output);
>                                 long tim = System.currentTimeMillis();
>                                 Random random = new Random(tim);
>                                 int RandomNumber = random.nextInt(9);
>                                 a.setText(RandomNumber);
>                         }
>                 });
>     }
>
> }
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to