导致类android.widget.GridLayout应用崩溃的错误

时间:2018-09-15 16:30:32

标签: java android android-emulator

当我尝试在Android Studio模拟器中运行我的应用程序时,一切正常,但是,当我尝试在手机(LG G4)上运行该应用程序时,崩溃并且日志中的错误是:

  

膨胀类android.widget.GridLayout的错误

这是我的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">

    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="368dp"
        android:layout_height="360dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/board"
        android:columnCount="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:rowCount="3">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:onClick="dropIn"
            android:tag="0"
            app:layout_column="0"
            app:layout_row="0" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:onClick="dropIn"
            android:tag="1"
            app:layout_column="1"
            app:layout_row="0" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:onClick="dropIn"
            android:tag="2"
            app:layout_column="2"
            app:layout_row="0" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_margin="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="25dp"
            android:onClick="dropIn"
            android:tag="3"
            app:layout_column="0"
            app:layout_row="1" />

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="25dp"
            android:onClick="dropIn"
            android:tag="4"
            app:layout_column="1"
            app:layout_row="1" />

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="25dp"
            android:onClick="dropIn"
            android:tag="5"
            app:layout_column="2"
            app:layout_row="1" />

        <ImageView
            android:id="@+id/imageView9"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="25dp"
            android:onClick="dropIn"
            android:tag="6"
            app:layout_column="0"
            app:layout_row="2" />

        <ImageView
            android:id="@+id/imageView10"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="25dp"
            android:onClick="dropIn"
            android:tag="7"
            app:layout_column="1"
            app:layout_row="2" />

        <ImageView
            android:id="@+id/imageView11"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="25dp"
            android:onClick="dropIn"
            android:tag="8"
            app:layout_column="2"
            app:layout_row="2" />
    </GridLayout>

    <LinearLayout
        android:id="@+id/playAgainLayout"
        android:layout_width="189dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="100dp"
        android:layout_marginTop="8dp"
        android:background="#46d65e"
        android:orientation="vertical"
        android:padding="30dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/gridLayout"
        app:layout_constraintHorizontal_bias="0.011"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/winnerMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="TextView"
            android:textSize="30sp" />

        <Button
            android:id="@+id/playAgainButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="playAgain"
            android:text="Play Again" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

这是我的java:

package com.k_pc.tick_tac_toe;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    // 0 = yellow, 1 = red;
    int activePlayer = 0;

    // 2 means unplayed
    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}};

    public void dropIn (View view){

        ImageView counter = (ImageView) view;

        System.out.println(counter.getTag().toString());

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

        if (gameState[tappedCounter] == 2) {

            gameState[tappedCounter] = activePlayer;

            counter.setTranslationY(-1000f);

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

            counter.animate().translationYBy(1000f).rotation(360f).setDuration(500);

            for (int [] winningPosition : winningPositions){
                if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
                        gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
                        gameState[winningPosition[0]] != 2){

                    //Someone has won

                    String winner = "Red";

                    if (gameState[winningPosition[0]] == 0){

                        winner = "Yellow";
                    }



                    TextView winnerMeassage = (TextView) findViewById(R.id.winnerMessage);
                    winnerMeassage.setText(winner + " has won!");

                    LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);

                    layout.setVisibility(View.VISIBLE);

                }
            }

        }

    }

    public void playAgain (View view){

        LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);

        layout.setVisibility(View.INVISIBLE);

        // 0 = yellow, 1 = red;
        activePlayer = 0;

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

        // setting all the images source to nothing


        GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);




        for(int i = 0 ; i<gridLayout.getChildCount() ; i++){

            ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
        }
    }

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

我认为代码无济于事,但是如果您想查看它,就在这里。

2 个答案:

答案 0 :(得分:1)

请检查您的可绘制文件夹。有时,当您的图像位于v24可绘制文件夹中时,会发生此错误,请将图像移至可绘制文件夹。

答案 1 :(得分:0)

尝试使用支持库版本。

通过gradle添加 implementation com.android.support:gridlayout-v7:27.1.1

<GridLayout>替换为<android.support.v7.widget.GridLayout>

还要更改导入以支持库