0

I don't know why I am getting this error in Android Studio, this is quite a simple command that I am executing. The code is provided below. Any help is greatly appreciated. I am getting the error in the last line of code.

 String[] sadThought = {"a", "sad", "Thought"};
 String[] sadRandom ={(sadThought[new Random().nextInt(sadThought.length)])};

    TvSad.setText(sadRandom);
2
  • It is a textview, I don't know what else to provide. Commented Jul 23, 2015 at 15:35
  • I would say from the time these variables came into existence till the code you have shown. (at least written if you can't post the code. Commented Jul 23, 2015 at 15:50

2 Answers 2

1

Simply use this

TvSad.setText(sadThought[new Random().nextInt(sadThought.length)]);
Sign up to request clarification or add additional context in comments.

7 Comments

It makes it easier, it works, but if anyone knows why the error is coming, I want to know
@user5145575 Again, What is TvSad??
@tnw the error was sadRandom is define as a array and he is passing an String array to textView , and textView is expecting String
@user5145575 you were defining sadRandom as an String array but actually you should have define as a String because textView takes a String in textView.SetText() as a Parameter
Thank you everyone, I'm sorry I am new
|
0

You dont have to create a separate array to get a random string from your sadThought array. You can try:

TvSad.setText(sadThought[new Random().nextInt(sadThought.length)]);

Also, make sure your TvSad is an instance of textView or EditText or other widgets that support .setText(..) method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.