Every time I try to start my app it crashes leaving this message in 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 AdapterViewCaused by: java.lang.UnsupportedOperationException: addView(View,
LayoutParams) is not supported in AdapterView
This is my code:
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>
2
Answers
your
GridView
is lackingnumColumns
property and also its children must havelayout_width="match_parent"
. for example:Your problem is that you can’t add children to a
<GridView>
(or otherAdapterView
subclasses, such asListView
) directly in XML. You need to create aListAdapter
and create the items using that from Java/Kotlin code.However, that’s probably not what you want if you’re trying to create a grid in XML. You might be looking for a
GridLayout
instead.