Я очень новичок в Android.Я хотел бы напечатать это в TextView, но экран весь белый, и я не могу видеть содержимое TextView.В консоли работает нормально.Ниже приведен файл моей деятельности и макета.
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fruits();
}
public void Fruits() {
textView= findViewById(R.id.pa);
String[] fruit = {"orange", "apple", "pear", "bannana", "strawberry", "mango","grape","lemon"};
Random numberGenerator = new Random();
/* Generate A Random Number */
int nextRandom = numberGenerator.nextInt(fruit.length)
;
Set<Integer> validate = new HashSet<>();
/* Add First Randomly Genrated Number To Set */
validate.add(nextRandom);
for (int i = 0; i < fruit.length; i++) {
/* Generate Randoms Till You Find A Unique Random Number */
while(validate.contains(nextRandom)) {
nextRandom = numberGenerator.nextInt(fruit.length);
}
/* Add Newly Found Random Number To Validate */
validate.add(nextRandom);
System.out.println(fruit[nextRandom]);
textView.setText(fruit[nextRandom]);
}
}
}
макет
<TextView
android:id="@+id/pa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />