从GridLayout获取ImageView子级并清除它们?

时间:2018-10-16 19:39:59

标签: android

我正在尝试使用一个教程来构建Tic Tac Toe应用程序。我被困在一个部分。我有一个GridLayout,它在3行和3列中有9个ImageView。看看我的activity_main xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="315dp"
    android:layout_height="333dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="@drawable/matrix"
    app:columnCount="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.454"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.562"
    app:rowCount="3">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="82dp"
        android:layout_height="70dp"
        android:layout_marginTop="18dp"
        android:layout_marginBottom="10dp"
        android:onClick="onclick"
        android:tag="0"
        app:layout_column="0"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="130dp"
        android:layout_height="73dp"
        android:layout_marginTop="15dp"
        android:onClick="onclick"
        android:tag="1"
        app:layout_column="1"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="87dp"
        android:layout_height="74dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:onClick="onclick"
        android:tag="2"
        app:layout_column="2"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="82dp"
        android:layout_height="70dp"
        android:layout_marginTop="18dp"
        android:layout_marginBottom="10dp"
        android:onClick="onclick"
        android:tag="3"
        app:layout_column="0"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="130dp"
        android:layout_height="73dp"
        android:layout_marginTop="15dp"
        android:onClick="onclick"
        android:tag="4"
        app:layout_column="1"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="87dp"
        android:layout_height="74dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:onClick="onclick"
        android:tag="5"
        app:layout_column="2"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="82dp"
        android:layout_height="70dp"
        android:layout_marginTop="22dp"
        android:layout_marginBottom="10dp"
        android:onClick="onclick"
        android:tag="6"
        app:layout_column="0"
        app:layout_row="2" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="130dp"
        android:layout_height="73dp"
        android:layout_marginTop="20dp"
        android:onClick="onclick"
        android:tag="7"
        app:layout_column="1"
        app:layout_row="2" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="87dp"
        android:layout_height="74dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="15dp"
        android:onClick="onclick"
        android:tag="8"
        app:layout_column="2"
        app:layout_row="2" />
</android.support.v7.widget.GridLayout>

<LinearLayout
    android:id="@+id/linearlayout"
    android:layout_width="276dp"
    android:layout_height="176dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.355">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="93dp"
        android:text="TextView"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:onClick="playagain"
        android:text="@string/button"
        android:textSize="24sp" />
</LinearLayout>

要清空GridLayout中已经绘制的图像,我使用了此功能。但是应用崩溃了,有人可以告诉我它是否正确

enter code here
public void playagain(View view)
 {
   GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);

    for (int i = 0; i< gridLayout.getChildCount(); i++)
    {
        ((ImageView)gridLayout.getChildAt(i)).setImageResource(0);
    }

 }

Here's the image of my logcat

2 个答案:

答案 0 :(得分:1)

您必须导入android.support.v7.widget.GridLayout,而不要在课程中导入android.widget.GridLayout

答案 1 :(得分:0)

吃什么? 您可以尝试从以下位置更改代码

GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);android.support.v7.widget.GridLayout gridLayout = (android.support.v7.widget.GridLayout)findViewById(R.id.gridLayout); 使用

  

android.support.v7.widget.GridLayout

代替

  

GridLayout