.setBackgroundResource(0)使我的应用程序崩溃

时间:2019-01-17 18:08:00

标签: java android android-layout

在应用程序中,我将资源分配给GridLayout中的按钮。

if(fill[tappedButtonLocation] == 2 && !gameOver) {
            fill[tappedButtonLocation] = activePlayer;
            if (activePlayer == 0) {
                tappedButton.setBackgroundResource(R.drawable.circle_yellow);
                activePlayer = 1;
            } else {
                tappedButton.setBackgroundResource(R.drawable.circle_yelp);
                activePlayer = 0;
            }
        }

然后,我想删除资源,但它会使应用程序崩溃。

    GridLayout gridLayout = findViewById(R.id.gridLayout);
    for(int i=0; i<gridLayout.getChildCount();i++){
        ((Button) gridLayout.getChildAt(i)).setBackgroundResource(0);
    }

分配的资源是

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#FFFC00"> </solid>
    <size android:height="120dp"
        android:width="120dp"> </size>
</shape>

Logcat错误:

java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:6597) 
        at android.view.View.performClickInternal(View.java:6574) 
        at android.view.View.access$3100(View.java:778) 
        at android.view.View$PerformClick.run(View.java:25885) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
     Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
        at com.example.tictokgame.MainActivity.playAgain(MainActivity.java:65)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:6597) 
        at android.view.View.performClickInternal(View.java:6574) 
        at android.view.View.access$3100(View.java:778) 
        at android.view.View$PerformClick.run(View.java:25885) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

如何从GridView中删除资源。 Actually I want to reset the tic-toc-toe game after clicking on play again button.

1 个答案:

答案 0 :(得分:3)

问题似乎是堆栈跟踪中指出的ClassCastException

Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout

请在执行以下操作的类中检查您的导入:

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

更改此:

import android.widget.GridLayout;

收件人:

import android.support.v7.widget.GridLayout;