点击ImageView会崩溃应用程序

时间:2017-12-21 21:05:55

标签: java android nullpointerexception

我认为问题出在counter.getTag().toString();行。我尝试将其放入字符串中,然后将其传递给tappedCounter,但我的应用仍会崩溃。

如果有人帮我解决如何使用Android Studio中的logcat来解决这类问题,那将非常有帮助。

这是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文件: -

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

2 个答案:

答案 0 :(得分:-1)

您没有在应用或xml中的任何位置设置标记。所以getTag将返回null。如果您希望视图包含标记,则必须添加它。

答案 1 :(得分:-1)

logcat的引发者:部分标识崩溃的原因。如果您仔细阅读logcat,崩溃的最后一个原因是NullPointerException

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

现在,您尝试使用getTag()获取代码的值,但您尚未在XML布局中的任何ImageView上设置任何代码。因此,当null被调用时,标记为toString抛出异常。