调用Button对象崩溃和挂起过程的方法

时间:2016-01-04 16:12:12

标签: java android nullpointerexception

我正在尝试在Android Studio中的setText实例上运行一个运行setEnabledButton的小方法。 问题是前者导致崩溃,后者导致进程挂起/冻结。 日志指向NullPointerException,但是我在调​​用之前创建了对象。 代码首先......

package com.example.richardcurteis.connect3;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import java.util.Random;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public boolean noughtsTurn;
    ArrayList board;
    Players player;

    public void receiveClick(View view) {
        String buttonPressed = (String) view.getTag();
        board.remove(buttonPressed);

       if (view instanceof Button) {
           Button b = (Button) view;
           b.setText(noughtsTurn ? player.noughtsPlayer() : player.crossesPlayer()); // Crashes program
           b.setEnabled(false); // Hangs program
           System.out.println(board);
       }
    }

    public class Players {
        public String noughtsPlayer() { return "O"; }
        public String crossesPlayer() { return "X"; }
        public String blankButton() { return ""; }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        boolean noughtsTurn = true;
        board = new ArrayList();
        for (int x = 0; x < getBoardSize(); x++) {
            String y = String.valueOf(x);
            board.add(y);
        }
        Players player = new Players();

    }

    public int getBoardSize() {
        int buttonCount = 0;
        TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);

        for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) {
            View tableLayoutChild = tableLayout.getChildAt(rowIndex);
            if (tableLayoutChild instanceof TableRow) {
                for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) {
                    View view = ((ViewGroup) tableLayoutChild).getChildAt(i);
                    if (view instanceof Button) {
                        buttonCount++;
                    }
                }
            }
        }
        return buttonCount;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我看不出第一组日志中的情况。 由于setText()崩溃日志中的这一行,我很确定按钮是这里的问题: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) connectMainActivity$Players.crossesPlayer()' on a null object reference receiveClicMainActivity.java:30)

Log of hang/freeze from setEnabled() method

01-04 15:59:19.322 10586-10586/? I/art: Not late-enabling -Xcheck:jni (already on)
01-04 15:59:19.464 10586-10592/? I/art: Debugger is no longer active
01-04 15:59:19.471 10586-10586/? W/System: ClassLoader referenced unknown path: /data/app/com.example.richardcurteis.connect3-2/lib/x86
01-04 15:59:19.952 10586-10604/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-04 15:59:20.015 10586-10604/? I/OpenGLRenderer: Initialized EGL, version 1.4
01-04 15:59:20.067 10586-10604/? W/EGL_emulation: eglSurfaceAttrib not implemented
01-04 15:59:20.068 10586-10604/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabe3fec0, error=EGL_SUCCESS
01-04 15:59:20.813 10586-10586/com.example.richardcurteis.connect3 I/Choreographer: Skipped 37 frames!  The application may be doing too much work on its main thread.
01-04 15:59:23.066 10586-10586/com.example.richardcurteis.connect3 I/System.out: [1, 2, 3, 4, 5, 6, 7, 8]

Log of crash from setText() method

01-04 15:47:45.251 10356-10356/com.example.richardcurteis.connect3 I/art: Not late-enabling -Xcheck:jni (already on)
01-04 15:47:45.561 10356-10356/com.example.richardcurteis.connect3 W/System: ClassLoader referenced unknown path: /data/app/com.example.richardcurteis.connect3-1/lib/x86
01-04 15:47:45.949 10356-10367/com.example.richardcurteis.connect3 I/art: Background sticky concurrent mark sweep GC freed 22272(1011KB) AllocSpace objects, 0(0B) LOS objects, 71% free, 1048KB/3MB, paused 2.279ms total 154.536ms
01-04 15:47:45.956 10356-10367/com.example.richardcurteis.connect3 W/art: Suspending all threads took: 7.014ms
01-04 15:47:46.021 10356-10386/com.example.richardcurteis.connect3 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-04 15:47:46.262 10356-10386/com.example.richardcurteis.connect3 I/OpenGLRenderer: Initialized EGL, version 1.4
01-04 15:47:46.316 10356-10386/com.example.richardcurteis.connect3 W/EGL_emulation: eglSurfaceAttrib not implemented
01-04 15:47:46.316 10356-10386/com.example.richardcurteis.connect3 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad75fcc0, error=EGL_SUCCESS
01-04 15:47:50.647 10356-10356/com.example.richardcurteis.connect3 D/AndroidRuntime: Shutting down VM
01-04 15:47:50.648 10356-10356/com.example.richardcurteis.connect3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.richardcurteis.connect3, PID: 10356
java.lang.IllegalStateException: Could not execute method for android:onClick
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
     at android.view.View.performClick(View.java:5198)
     at android.view.View$PerformClick.run(View.java:21147)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5417)
     at java.lang.reflect.Method.invoke(Native Method)ruZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Method.invoke(Native Method) onClicAppCompatViewInflater.java:270)
     at android.view.View.performClick(View.java:5198) 
     at android.view.View$PerformClick.run(View.java:21147) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) ruZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) connectMainActivity$Players.crossesPlayer()' on a null object reference receiveClicMainActivity.java:30)
     at java.lang.reflect.Method.invoke(Native Method) onClicAppCompatViewInflater.java:270) 
     at android.view.View.performClick(View.java:5198) 
     at android.view.View$PerformClick.run(View.java:21147) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) ruZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
01-04 15:47:53.044 10356-10356/? I/Process: Sending signal. PID: 10356 SIG: 9

1 个答案:

答案 0 :(得分:2)

Players player = new Players();

这将创建一个名为player的局部变量,该变量与对象的player字段具有相同的名称。在onCreate方法中,声明此局部变量,然后立即让它超出范围,丢失值。同时,您的player字段仍然未初始化。

要修正错误,请将其更改为:

player = new Players();

这会设置player字段,而不是新的局部变量。