Java 简单网格布局缩放动画

Java 简单网格布局缩放动画,java,android,Java,Android,我正在尝试将GridLayout的X和Y从0缩放到100。我希望我的网格看起来“看不见”,然后通过改变它的比例使它变得非常大。但是,每次尝试运行代码时都会出现异常: gameGrid = (GridLayout) findViewById(R.id.gameView); ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1,

我正在尝试将GridLayout的X和Y从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));
对象不为null,但我得到以下异常:

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>

对象不为null,但我得到以下异常:

Attempt to invoke virtual method 'void android.widget.GridLayout.setLayoutAnimation(android.view.animation.LayoutAnimationController)' on a null object reference
您的
GridView
id是:

android:id="@+id/GameGrid"
你正在这样做:

gameGrid = (GridLayout) findViewById(R.id.gameView);
应该是这个

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

另外,尝试使用
setAnimation()
而不是
setlayouanimation()
,因为我正在工作。

好的,我是这样解决的:

   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();

你能发布你的xml吗?你看过堆栈跟踪了吗?我的意思是,在到达“gameGrid.SetLayoutImation(new GridLayoutImationController(animation));”行时,是否真的会发生异常?首先,我找到元素,然后尝试对其设置动画。怎么了?谢谢,但是在更改之后我得到了这样的信息:“id.view.animation.LayoutAnimationController$AnimationParameters不能强制转换为android.view.animation.GridLayoutAnimationController$AnimationParameters”哦,你也更改了它,我正在修改我的答案哈哈:D