【问题标题】:My Whole App Crashes When I Click My Play Again Button [closed]当我单击“再次播放”按钮时,我的整个应用程序崩溃
【发布时间】:2020-12-20 16:41:45
【问题描述】:

我正在制作类似于 tic tac toe 的东西,一切正常,但除此再次播放按钮之外的所有内容都无法正常工作,并使整个应用程序崩溃。我尝试再次制作它,并尝试制作一个单独的方法来在按下时记录它并且它正在工作,但它不能与再次播放按钮一起工作。

下面是我的播放按钮代码以及代码图片以及单击按钮时发生的错误。

public void playAgain(View view) {

    Button playButton = (Button) findViewById(R.id.playButton);

    TextView winnerView = (TextView) findViewById(R.id.winnerView);

    playButton.setVisibility(View.INVISIBLE);

    winnerView.setVisibility(View.INVISIBLE);

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

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

        ImageView counter = (ImageView) gridLayout.getChildAt(i);

        counter.setImageDrawable(null);

    }

    Arrays.fill(gameState, 2);

    activePlayer = 0;
    gameActive = true;

}

【问题讨论】:

  • 我会说您已经导入或使用了错误版本的GridLayout 作为XML 中的版本和代码中使用的版本不匹配。 (这基本上就是错误日志所说的。)
  • @MarkusKauppinen 是的,你是对的,我改了它,它正在工作。
  • 请将代码和堆栈跟踪作为文本粘贴到问题中。不要张贴文字图片。它们难以阅读且无法搜索。

标签: java android arrays xml


【解决方案1】:

视图有 androidx GridLayout 但在 Activity 内部,android GridLayout 被用于获取由于转换错误导致崩溃的视图。

我同意更改 Activity 中的导入将解决您的问题,但请确保您要根据需要使用 androidx GridLayout 而不是 Android GridLayout。

【讨论】:

    【解决方案2】:

    只需替换import android.widget.GridLayout;

    通过

    导入 androidx.gridlayout.widget.GridLayout;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      相关资源
      最近更新 更多