在 XML 中使用 GridView,获取 java.lang.UnsupportedOperationException: AdapterView 不支持 addView(View, LayoutParams)

Using GridView in XML, getting java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

每次我尝试启动我的应用程序时它都会崩溃并在 logcat

中留下这条消息

Caused by: android.view.InflateException: Binary XML file line #15 in com.example.connect3game:layout/activity_main: addView(View, LayoutParams) is not supported in AdapterView

Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

这是我的代码:

MainActivity.java

package com.example.connect3game;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    //0:yellow 1:Red

    int activePlayer = 0;

    int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2,};

    int [] [] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6,7,8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2,4, 6}};

    boolean gameActive = true;

    public void dropIn(View view){

        ImageView counter = (ImageView) view;

        int tappedCounter = Integer.parseInt(counter.getTag().toString());

        if(gameState[tappedCounter] == 2 && gameActive) {

            gameState[tappedCounter] = activePlayer;

            counter.setTranslationY(-1500);


            if (activePlayer == 0) {
                counter.setImageResource(R.drawable.yellow);
                activePlayer = 1;

            } else {
                counter.setImageResource(R.drawable.red);
                activePlayer = 0;

            }
            counter.animate().translationYBy(1500).setDuration(300);

            for (int[] winningPosition : winningPositions) {
                if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]] && gameState[winningPosition[0]] != 2) {
                    // someone has won
                    gameActive = false;


                    String winner = "";

                    if (activePlayer == 1) {
                        winner = "Yellow";

                    } else {
                        winner = "Red";

                    }
                    Toast.makeText(this, winner + " Has won!", Toast.LENGTH_SHORT).show();

                    Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
                    TextView textView = (TextView) findViewById(R.id.textView);
                    textView.setText(winner + "Has Won!");
                    textView.setVisibility(View.VISIBLE);
                    playAgainButton.setVisibility(View.VISIBLE);

                }

            }
        }
    }
    public void playAgain(View view){
        Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setVisibility(View.INVISIBLE);
        playAgainButton.setVisibility(View.INVISIBLE);
        GridView gridView = (GridView) findViewById(R.id.gridView);

        for(int i = 0; i < gridView.getChildCount(); i++) {
            ImageView counter = (ImageView) gridView.getChildAt(i);
            counter.setImageDrawable(null);

        }
        for (int i=0; i<gameState.length; i++){
            gameState[i] = 2;

        }

        int activePlayer = 0;

        int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2,};

        boolean gameActive = true;

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="dropIn"
            app:srcCompat="@drawable/grid" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="141dp"
            android:layout_height="101dp"
            android:onClick="dropIn"
            android:tag="4" />

        <ImageView
            android:id="@+id/imageView9"
            android:layout_width="141dp"
            android:layout_height="101dp"
            android:layout_marginTop="190dp"
            android:onClick="dropIn"
            android:tag="7" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="141dp"
            android:layout_height="101dp"
            android:layout_marginBottom="190dp"
            android:onClick="dropIn"
            android:tag="1" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="141dp"
            android:layout_height="101dp"
            android:layout_marginEnd="280dp"
            android:onClick="dropIn"
            android:tag="3" />

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="141dp"
            android:layout_height="101dp"
            android:layout_marginTop="190dp"
            android:layout_marginEnd="280dp"
            android:onClick="dropIn"
            android:tag="6" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="141dp"
            android:layout_height="101dp"
            android:layout_marginEnd="280dp"
            android:layout_marginBottom="190dp"
            android:onClick="dropIn"
            android:tag="0" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="141dp"
            android:layout_height="101dp"
            android:layout_marginStart="280dp"
            android:onClick="dropIn"
            android:tag="5" />

        <ImageView
            android:id="@+id/imageView10"
            android:layout_width="141dp"
            android:layout_height="101dp"
            android:layout_marginStart="280dp"
            android:layout_marginTop="190dp"
            android:onClick="dropIn"
            android:tag="8" />

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="141dp"
            android:layout_height="101dp"
            android:layout_marginStart="280dp"
            android:layout_marginBottom="190dp"
            android:onClick="dropIn"
            android:tag="2" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Play Again!"
            android:textSize="24sp"
            android:visibility="invisible" />

        <Button
            android:id="@+id/playAgainButton"
            android:layout_width="130dp"
            android:layout_height="65dp"
            android:onClick="playAgain"
            android:text="Restart"
            android:textSize="18sp"
            android:visibility="invisible" />
    </GridView>

</androidx.constraintlayout.widget.ConstraintLayout>

您的 GridView 缺少 numColumns 属性 并且其子项必须具有 layout_width="match_parent"。例如:

<GridView
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:horizontalSpacing="6dp"
    android:numColumns="2"
    android:verticalSpacing="6dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:onClick="dropIn"
        app:srcCompat="@drawable/grid" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:onClick="dropIn"
        app:srcCompat="@drawable/grid" />

</GridView>

您的问题是您不能直接在 XML 中将子级添加到 <GridView>(或其他 AdapterView 子类,例如 ListView)。您需要创建一个 ListAdapter 并使用来自 Java/Kotlin 代码的项目创建项目。

但是,如果您尝试在 XML 中创建网格,那可能不是您想要的。您可能正在寻找 GridLayout