GridLayout находит представление по его идентификатору - PullRequest
0 голосов
/ 03 июля 2019

Я пытаюсь сосчитать дочерний элемент в сетке в Относительном макете, но он не работает. и когда я нажимаю кнопку перезагрузки, он говорит, что, к сожалению, игра остановилась. я пытаюсь пропустить изображения в сетке и перезапустить игру снова из новой.

public void restartGame(View view)
    {
        GridLayout gridLayout=(GridLayout)findViewById(R.id.gridLayout);
        for(int i=0;i<gridLayout.getChildCount();i++)
        {
            ((ImageView)gridLayout.getChildAt(i)).setImageResource(0);
        }

    }

здесь XML-файл. Пожалуйста, игнорируйте LinearLayout XML-код. работает нормально.

<RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

        <android.support.v7.widget.GridLayout
            android:id="@+id/gridLayout"
            android:layout_width="295dp"
            android:layout_height="260dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginBottom="48dp"
            android:background="@drawable/board"
            app:columnCount="3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.505"
            app:layout_constraintStart_toStartOf="parent"
            app:rowCount="3">

            //Image views are here 

        </android.support.v7.widget.GridLayout>

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@color/colorAccent"
            android:orientation="vertical"
            android:padding="15dp"
            android:visibility="invisible">

            <TextView
                android:id="@+id/winnerMessage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="who won?"
                android:textSize="24sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/restartButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="restartGame"
                android:text="Restart" />
        </LinearLayout>

    </RelativeLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...