Простая анимация масштаба GridLayout

Я пытаюсь масштабировать X и Y GridLayout от 0 до 100. Я хочу, чтобы моя сетка выглядела «невидимой», а затем я хочу, чтобы она была очень большой, изменяя ее масштаб. Однако я получаю исключение каждый раз, когда пытаюсь запустить свой код:

   gameGrid = (GridLayout) findViewById(R.id.gameView);
    ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1,
                                                   Animation.RELATIVE_TO_SELF, 0.5f,
                                                   Animation.RELATIVE_TO_SELF, 0.5f);
    gameGrid.setLayoutAnimation(new GridLayoutAnimationController(animation));

Объект не равен нулю, но я получаю исключение:

Attempt to invoke virtual method 'void android.widget.GridLayout.setLayoutAnimation(android.view.animation.LayoutAnimationController)' on a null object reference

Как мне решить эту проблему? Выглядит довольно просто, пожалуйста, чего мне здесь не хватает?

Мой файл XML:

    <?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:layout_gravity = "center"
    android:gravity = "center"
    android:layoutDirection = "ltr"
    android:orientation = "vertical"
    android:padding = "16dp">


  <GridLayout
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:layout_gravity = "center"
      android:columnCount = "7"
      android:orientation = "vertical"
      android:rowCount = "7"
      android:visibility = "visible"
      android:id = "@+id/GameGrid">

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B1"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B2"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B3"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B4"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B5"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B6"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B7"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />
    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B8"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B9"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B10"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B11"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B12"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B13"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B14"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B15"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B16"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B17"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B18"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B19"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B20"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B21"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B22"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B23"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B24"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B25"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B26"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B27"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B28"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B29"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B30"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B31"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B32"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B33"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B34"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B35"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B36"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B37"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B38"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B39"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B40"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B41"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B42"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

    </TableRow>

    <TableRow>

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B43"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B44"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B45"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B46"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B47"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B48"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

      <com.tastudio.doors123.SquareButton
          android:id = "@+id/B49"
          android:layout_margin = "3dp"
          android:layout_weight = "1"
          android:background = "@drawable/knight3" />

    </TableRow>
  </GridLayout>
</LinearLayout>

Не могли бы вы опубликовать свой xml, пожалуйста?

Skizo-ozᴉʞS 03.10.2018 21:01

Вы смотрели трассировку стека? Я имею в виду, действительно ли возникает исключение при достижении «gameGrid.setLayoutAnimation (new GridLayoutAnimationController (animation));» линия?

Itamar Kerbel 03.10.2018 21:23

Сначала я нахожу элемент, потом пытаюсь его оживить. Что случилось?

Tal Angel 03.10.2018 21:24
Пользовательский скаляр GraphQL
Пользовательский скаляр GraphQL
Листовые узлы системы типов GraphQL называются скалярами. Достигнув скалярного типа, невозможно спуститься дальше по иерархии типов. Скалярный тип...
Как вычислять биты и понимать побитовые операторы в Java - объяснение с примерами
Как вычислять биты и понимать побитовые операторы в Java - объяснение с примерами
В компьютерном программировании биты играют важнейшую роль в представлении и манипулировании данными на двоичном уровне. Побитовые операции...
Поднятие тревоги для долго выполняющихся методов в Spring Boot
Поднятие тревоги для долго выполняющихся методов в Spring Boot
Приходилось ли вам сталкиваться с требованиями, в которых вас могли попросить поднять тревогу или выдать ошибку, когда метод Java занимает больше...
Полный курс Java для разработчиков веб-сайтов и приложений
Полный курс Java для разработчиков веб-сайтов и приложений
Получите сертификат Java Web и Application Developer, используя наш курс.
2
3
81
2
Перейти к ответу Данный вопрос помечен как решенный

Ответы 2

Ответ принят как подходящий

The object is not null, but I get this exception:

Ваш идентификатор GridView:

android:id = "@+id/GameGrid"

А вы делаете это:

gameGrid = (GridLayout) findViewById(R.id.gameView);

Это должно быть это

gameGrid = (GridLayout) findViewById(R.id.GameGrid);

Также попробуйте использовать setAnimation() вместо setLayoutAnimation(), у меня работает.

Спасибо, но после изменения я получаю следующее: «Параметры id.view.animation.LayoutAnimationController $ AnimationParame‌ не могут быть преобразованы в android.view.animation.GridLayoutAnimationController $ Animati onParameters»

Tal Angel 03.10.2018 21:32

О, ты тоже это изменил, я изменял свой ответ, ха-ха: D

Skizo-ozᴉʞS 03.10.2018 21:46

Хорошо, я решил это так:

   gameGrid = (GridLayout) findViewById(R.id.GameGrid);
    ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1,
                                                   Animation.RELATIVE_TO_SELF, 0.5f,
                                                   Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setDuration(1000);
    gameGrid.setAnimation(animation);
    gameGrid.animate();

Другие вопросы по теме