Good evening!

Can you please tell Onclick(...) how to call the ArrayList method that was created in the OnCreate(...) method from the Onclick(...) method?

So far I have done this:

OnCreate (...) :

 ArrayList<Integer> Arrayl = new ArrayList<>(); for (int a = 1; a < 8; a++) { Arrayl.add(a); } Collections.shuffle(Arrayl);// перемешиваем 

In Onclick (...) :

 int actvopros= Arrayl.get(0); //и такое обращение не работает 

    2 answers 2

    Most likely, your problem is that you declare the Arrayl collection as a local variable of the OnCreate(...) method, which will not be visible outside of this method.

    In order to access this variable from other methods (of the same class), the Arrayl collection Arrayl be declared as a class field:

     public class MainActivity extends AppCompatActivity { private ArrayList<Integer> Arrayl; @Override protected void onCreate(Bundle savedInstanceState) { Arrayl = new ArrayList<>(); for (int a = 1; a < 8; a++) { Arrayl.add(a); } Collections.shuffle(Arrayl); } } 

      Declare out of methods, initialize in OnCreate and use where you want.

       class A { private ArrayList Arrayl; ... OnCreate { ... Array1 = new ArrayList<>() .... } void B { Array1 будет видно. } } 

      Like this.