gpt4 book ai didi

java - 我使用了保留字吗?

转载 作者:行者123 更新时间:2023-12-01 19:26:56 26 4
gpt4 key购买 nike

我是 Android 开发新手,最近开始了 Udemy 类(class)。当我尝试制作一个简单的游戏时,在引用 TextView 时遇到了问题。

这是我的 MainActivity.java 代码:


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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}};

int activePlayer = 0;

boolean gameActive = true;

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

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

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.red);

activePlayer = 1;

} else {

counter.setImageResource(R.drawable.yellow);

activePlayer = 0;

}

counter.animate().translationYBy(1500).rotation(3600).setDuration(300);

for (int[] winningPosition : winningPositions) {

if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]] && gameState[winningPosition[0]] != 2) {

String winner = "";

if (activePlayer == 1) {

winner = "Red";

} else {

winner = "Yellow";

}

Toast.makeText(this, winner + " has won!", Toast.LENGTH_LONG).show();

gameActive = false;

}

}

if (!gameActive) {

hasWon.setAlpha(1);

playAgain.setAlpha(1);

playAgain.setClickable(true);

}

} else {

Toast.makeText(this, "No Fair!", Toast.LENGTH_SHORT).show();

}

}

public void restartGame(View view) {

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

Toast.makeText(this, "Restarted!", Toast.LENGTH_SHORT).show();

}

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

hasWon.setAlpha(0);

playAgain.setAlpha(0);

playAgain.setClickable(true);

}
}

这是我的错误:

22396-22396/com.company321.connect3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company321.connect3, PID: 22396
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.company321.connect3/com.company321.connect3.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:655)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.company321.connect3.MainActivity.<init>(MainActivity.java:25)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

它指向这一行:

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

请帮我解决这个问题并告诉我我做错了什么以及我是否使用了任何保留字

最佳答案

这是您的更新代码:

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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}};

int activePlayer = 0;

boolean gameActive = true;
TextView hasWon;
Button playAgain;

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.red);

activePlayer = 1;

} else {

counter.setImageResource(R.drawable.yellow);

activePlayer = 0;

}

counter.animate().translationYBy(1500).rotation(3600).setDuration(300);

for (int[] winningPosition : winningPositions) {

if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]] && gameState[winningPosition[0]] != 2) {

String winner = "";

if (activePlayer == 1) {

winner = "Red";

} else {

winner = "Yellow";

}

Toast.makeText(this, winner + " has won!", Toast.LENGTH_LONG).show();

gameActive = false;

}

}

if (!gameActive) {

hasWon.setAlpha(1);

playAgain.setAlpha(1);

playAgain.setClickable(true);

}

} else {

Toast.makeText(this, "No Fair!", Toast.LENGTH_SHORT).show();

}

}

public void restartGame(View view) {

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

Toast.makeText(this, "Restarted!", Toast.LENGTH_SHORT).show();

}

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

hasWon = (TextView) findViewById(R.id.hasWon);
playAgain = (Button) findViewById(R.id.playAgain);

hasWon.setAlpha(0);

playAgain.setAlpha(0);

playAgain.setClickable(true);

}
}

关于java - 我使用了保留字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61260833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com