-2

I think the problem is in the line counter.getTag().toString();. I tried to put it in a string and then pass it to tappedCounter, but still my app crashes.

It would be highly helpful if someone helps me on how to figure out ourselves using the logcat in Android Studio on solving problems like this.

Here is the logcat : -

12-22 02:09:59.790 14251-14251/com.example.msq.practice E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.msq.practice, PID: 14251
                                                                          java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                              at android.view.View.performClick(View.java:6256)
                                                                              at android.view.View$PerformClick.run(View.java:24701)
                                                                              at android.os.Handler.handleCallback(Handler.java:789)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                              at android.os.Looper.loop(Looper.java:164)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                           Caused by: java.lang.reflect.InvocationTargetException
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                              at android.view.View.performClick(View.java:6256) 
                                                                              at android.view.View$PerformClick.run(View.java:24701) 
                                                                              at android.os.Handler.handleCallback(Handler.java:789) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:98) 
                                                                              at android.os.Looper.loop(Looper.java:164) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                              at com.example.msq.practice.MainActivity.click(MainActivity.java:27)
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                              at android.view.View.performClick(View.java:6256) 
                                                                              at android.view.View$PerformClick.run(View.java:24701) 
                                                                              at android.os.Handler.handleCallback(Handler.java:789) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:98) 
                                                                              at android.os.Looper.loop(Looper.java:164) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

MainActivity :-

    package com.example.msq.practice;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.view.View;

    public class MainActivity extends AppCompatActivity {

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

        int activePlayer = 0;
        int[] gameState = {2,2,2,2,2,2,2,2,2};
        int[][] winningPositions = {{1,2,3}, {4,5,6}, {7,8,9}, {0,3,6},                 
        {1,4,7,}, {2,5,8}, {0,4,8}, {2,4,6}};
        boolean isGameActive = true;
        String result;
        String tag;

        public void click(View view){

            ImageView counter = (ImageView) view;

            tag = counter.getTag().toString();

            int tappedCounter = Integer.parseInt(tag);

            if(gameState[tappedCounter] == 2 && isGameActive == true){

                counter.setTranslationY(-5000f);

                gameState[tappedCounter] = activePlayer;
                if(activePlayer == 0){
                    counter.setImageResource(R.drawable.circle);
                    activePlayer = 1;
                }
                else{
                    counter.setImageResource(R.drawable.cross);
                    activePlayer = 0;
                }



 counter.animate().rotation(360f).translationYBy(5000f).setDuration(1000);

    }

    for(int[] winningPosition : winningPositions){

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

            isGameActive = false;

            if(gameState[winningPosition[0]]==0){
                result = "PLayer 1 Won !";
            }
            else{
                result = "PLayer 2 Won !";
            }

        }
        else{

            boolean isGameOver = true;
            for(int counterState : gameState){
                if(counterState == 2) isGameOver = false;
            }

            if(isGameOver)
                result = "It's a Draw";

        }
    }
}

    }

activity_main.xml file :-

   <?xml version="1.0" encoding="utf-8"?>
   <android.support.constraint.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.msq.practice.MainActivity">



      <GridLayout


        android:id="@+id/gLayout"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="106dp"
        android:layout_marginEnd="42dp"
        android:layout_marginStart="42dp"
        android:layout_marginTop="105dp"
        android:background="@drawable/board"
        android:columnCount="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:rowCount="3">

        <ImageView
            android:id="@+id/imageView0"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_column="0"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_row="0"
            android:contentDescription="@string/cross"
            android:onClick="click" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_column="1"
            android:layout_marginStart="20dp"
            android:layout_marginTop="10dp"
            android:layout_row="0"
            android:contentDescription="@string/cross"
            android:onClick="click" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_column="0"
            android:layout_marginStart="10dp"
            android:layout_marginTop="22dp"
            android:layout_row="1"
            android:contentDescription="@string/cross"
            android:onClick="click" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_column="1"
            android:layout_marginStart="20dp"
            android:layout_marginTop="22dp"
            android:layout_row="1"
            android:contentDescription="@string/cross"
            android:onClick="click" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_column="2"
            android:layout_marginStart="20dp"
            android:layout_marginTop="22dp"
            android:layout_row="1"
            android:contentDescription="@string/cross"
            android:onClick="click"/>

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_column="1"
            android:layout_marginStart="20dp"
            android:layout_marginTop="22dp"
            android:layout_row="2"
            android:contentDescription="@string/cross"
            android:onClick="click" />

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_column="2"
            android:layout_marginStart="20dp"
            android:layout_marginTop="22dp"
            android:layout_row="2"
            android:contentDescription="@string/cross"
            android:onClick="click" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_column="0"
            android:layout_marginStart="10dp"
            android:layout_marginTop="22dp"
            android:layout_row="2"
            android:contentDescription="@string/cross"
            android:onClick="click" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_column="2"
            android:layout_marginStart="22dp"
            android:layout_marginTop="10dp"
            android:layout_row="0"
            android:contentDescription="@string/cross"
            android:onClick="click"/>


    </GridLayout>
</android.support.constraint.ConstraintLayout>
Zoe
  • 27,060
  • 21
  • 118
  • 148
Msq-9
  • 1
  • 1

2 Answers2

-1

You aren't setting a tag anywhere in the app or the xml. So getTag will return null. If you want a view to have a tag, you have to add it.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
-1

The Caused By: portion of a logcat identifies the cause of the crash. If you read the logcat carefully, the last cause of the the crash is a NullPointerException.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

Now you are trying to get the value of the tag using getTag(), but you haven't set any tag on any of the ImageView in your XML layout. Hence the tag is null throwing the exception when toString is called.

Kartik Arora
  • 571
  • 4
  • 10