Home > Enterprise >  onclick method in android studio is not working
onclick method in android studio is not working

Time:12-27

On the click of button the app is not working and the app is being crash. In the xml file , on the gridlayout I have created button, the working of those button is same for that I have use onClick method, but it is not working

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

for those button I have create check method in the java file Here is the java file

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

        }

    }

the buttons are not working. when I try to run the app, and click the button the app has crashed. The error I am getting

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

CodePudding user response:

What is b1 = btn1.getTag().toString(); It should be getText().toString();

CodePudding user response:

It seems you are trying to invoke the "equals()" method on a Null object. Which mean, one of those objects is not initialized when you try to call "equals()".

For example, I don't see anywhere in the code the variable "b7" being initialized before used in this portion of your code:

else if (b7.equals(b8) && b8.equals(b9) && !b9.equals("")) {
                Toast.makeText(this, "Winner is "   b7, Toast.LENGTH_LONG).show();
                //3
            }

Did you initialized these variables anywhere else?

Tip: Whenever you get this type of errors, use the debug tool in Android Studio, and check where the exception is happening in the code.

Looking at the code, I understand you mean to have the value from "btn7" stored in "b7" for example. In order for the program to work, you need to do that before using the variable "b7":

b7 = btn7.getText().toString()

CodePudding user response:

I fount it, I found your problem, In your condition if (count > 4), if true than your b1,b2, b3..... will be initialize, if false these variable will never be initialize, and in your case if (count > 4) return false, so b7, b8, b9 are not initalize and it cause null object reference

  • Related