点击按钮时,应用程序不工作,应用程序崩溃。在xml文件中,在我创建的网格布局按钮上,这些按钮的工作与我使用onClick方法相同,但它不工作
<GridLayout
android:id="@+id/gridlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:layout_centerInParent="true">
<androidx.appcompat.widget.AppCompatButton
android:onClick="check"
android:clickable="true"
android:id="@+id/btn1">
</androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn2"
android:clickable="true"
android:onClick="check"
android:layout_marginLeft="10dp">
</androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
android:onClick="check"
android:clickable="true"
android:id="@+id/btn3"
android:layout_marginLeft="10dp">
</androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
android:onClick="check"
android:clickable="true"
android:id="@+id/btn4">
</androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
android:onClick="check"
android:clickable="true"
android:id="@+id/btn5"
android:layout_marginLeft="10dp">
</androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
android:onClick="check"
android:clickable="true"
android:id="@+id/btn6"
android:layout_marginLeft="10dp">
</androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
android:onClick="check"
android:clickable="true"
android:id="@+id/btn7">
</androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
android:onClick="check"
android:clickable="true"
android:id="@+id/btn8"
android:layout_marginLeft="10dp">
</androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
android:onClick="check"
android:clickable="true"
android:id="@+id/btn9"
android:layout_marginLeft="10dp">
</androidx.appcompat.widget.AppCompatButton>
</GridLayout>
对于这些按钮,我在java文件中创建了检查方法,下面是java文件
private void findid() {
// this method is for find the id of evey reference variable
btn1=findViewById(R.id.btn1);
btn2=findViewById(R.id.btn2);
btn3=findViewById(R.id.btn3);
btn4=findViewById(R.id.btn4);
btn5=findViewById(R.id.btn5);
btn6=findViewById(R.id.btn6);
btn7=findViewById(R.id.btn7);
btn8=findViewById(R.id.btn8);
btn9=findViewById(R.id.btn9);
}
public void restart() {
count = 0;
flag = 0;
btn1.setText("");
btn2.setText("");
btn3.setText("");
btn4.setText("");
btn5.setText("");
btn6.setText("");
btn7.setText("");
btn8.setText("");
btn9.setText("");
}
public void check(View v) {
// this method is for checking the action of the button
AppCompatButton btncurrent = (AppCompatButton) v;
if (btncurrent.getText().toString().equals("")) {
count++;
if (flag == 0) {
btncurrent.setText("X");
flag = 1;
} else {
btncurrent.setText("O");
flag = 0;
}
if (count > 4) {
//after 4th move there is a chance to wining
b1 = btn1.getText().toString();
b2 = btn2.getText().toString();
b3 = btn3.getText().toString();
b4 = btn4.getText().toString();
b5 = btn5.getText().toString();
b6 = btn6.getText().toString();
b7 = btn7.getText().toString();
b8 = btn8.getText().toString();
b9 = btn9.getText().toString();
//check condition
//horinzontal conditions
if (b1.equals(b2) && b2.equals(b3) && !b1.equals("")) {
//1
Toast.makeText(this, "Winner is " + b1, Toast.LENGTH_LONG).show();
} else if (b4.equals(b5) && b5.equals(b6) && !b6.equals("")) {
Toast.makeText(this, "Winner is " + b4, Toast.LENGTH_LONG).show();
//2
}
} else if (b7.equals(b8) && b8.equals(b9) && !b9.equals("")) {
Toast.makeText(this, "Winner is " + b7, Toast.LENGTH_LONG).show();
//3
}
//vartical conditions
else if (b1.equals(b4) && b4.equals(b7) && !b4.equals("")) {
Toast.makeText(this, "Winner is " + b1, Toast.LENGTH_LONG).show();
//4
} else if (b2.equals(b5) && b5.equals(b8) && !b8.equals("")) {
Toast.makeText(this, "Winner is " + b5, Toast.LENGTH_LONG).show();
//5
} else if (b3.equals(b6) && b6.equals(b9) && !b9.equals("")) {
Toast.makeText(this, "Winner is " + b3, Toast.LENGTH_LONG).show();
//6
}
//Dioganal right
else if (b1.equals(b5) && (b5.equals(b9) && !b9.equals(""))) {
Toast.makeText(this, "Winner is " + b1, Toast.LENGTH_LONG).show();
//7
}
//Dioganal left
else if (b3.equals(b5) && b5.equals(b7) && !b7.equals("")) {
Toast.makeText(this, "Winner is " + b3, Toast.LENGTH_LONG).show();
}
else if (count==9){
Toast.makeText(this, "Match draw", Toast.LENGTH_SHORT).show();
}
}
}
按钮不工作。当我尝试运行应用程序,并点击按钮的应用程序已崩溃。错误我得到
--------- beginning of crash
2022-12-27 15:34:48.786 21154-21154/com.sumita.tic_tac_toe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sumita.tic_tac_toe, PID: 21154
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5458)
at android.view.View.performClick(View.java:6412)
at android.view.View$PerformClick.run(View.java:25341)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6977)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5453)
at android.view.View.performClick(View.java:6412)
at android.view.View$PerformClick.run(View.java:25341)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6977)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.sumita.tic_tac_toe.MainActivity.check(MainActivity.java:89)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5453)
at android.view.View.performClick(View.java:6412)
at android.view.View$PerformClick.run(View.java:25341)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6977)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)
2022-12-27 15:34:48.805 21154-21154/com.sumita.tic_tac_toe I/Process: Sending signal. PID: 21154 SIG: 9
3条答案
按热度按时间xzlaal3s1#
我发现了,我发现了你的问题,在你的条件
if (count > 4)
中,如果true
,那么你的b1,b2, b3.....
将被初始化,如果false
,这些变量将永远不会被初始化,而在你的情况if (count > 4) return false
中,所以b7, b8, b9
不会被初始化,这会导致null object reference
yebdmbv42#
什么是
b1 = btn1.getTag().toString();
应该是getText().toString();gojuced73#
看起来你正在尝试调用一个空对象的“equals()”方法。这意味着,当你尝试调用“equals()"时,其中一个对象没有初始化。
例如,我在代码中的任何地方都没有看到变量“b7”在用于代码的这一部分之前被初始化:
你在别的地方初始化过这些变量吗?
提示:每当您遇到此类错误时,请使用Android Studio中的调试工具,并检查代码中发生异常的位置。
看了代码,我明白你的意思是把“btn7”的值存储在“b7”中,为了让程序正常运行,你需要在使用变量“b7”之前这样做: