我正在编写一个android应用程序,并且结束了第二个活动。

public class OneBoardActivity extends Activity {
    //omitted
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_board);

    turn=true;
    statetext=(TextView)findViewById(R.id.StateText);
    mover[0][2]=(Button)findViewById(R.id.mover13);
    mover[0][3]=(Button)findViewById(R.id.mover14);
    mover[0][4]=(Button)findViewById(R.id.mover15);
            //omitted again
            for(i=0;i<7;i++){
        for(j=0;j<7;j++){
            if(i<=1 && j<=1 || i>=6 && j<=1 ||
                    i<=1 && j>=6 || i>=6 && j>=6){
                moverState[i][j]=-2;
                continue;
        }
        mover[i][j].setOnClickListener(new OnClickListener(){ //Line 92
        @Override
        public void onClick(View v) {
            // TODO Move by State
            moveByState(i,j);
        }
    });
    //omitted for the last time


...并且此代码结束,没有任何语法错误。
但是,当我运行此应用程序并退出此活动时,此应用程序导致了“不幸的是,...”错误。

这是logcat:

10-16 04:09:56.641: E/AndroidRuntime(1230): FATAL EXCEPTION: main
10-16 04:09:56.641: E/AndroidRuntime(1230): Process: com.truefalseapps.solitaire, PID: 1230
10-16 04:09:56.641: E/AndroidRuntime(1230): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.truefalseapps.solitaire/com.truefalseapps.solitaire.OneBoardActivity}: java.lang.NullPointerException
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.os.Looper.loop(Looper.java:136)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at java.lang.reflect.Method.invoke(Method.java:515)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at dalvik.system.NativeStart.main(Native Method)
10-16 04:09:56.641: E/AndroidRuntime(1230): Caused by: java.lang.NullPointerException
10-16 04:09:56.641: E/AndroidRuntime(1230):     at com.truefalseapps.solitaire.OneBoardActivity.onCreate(OneBoardActivity.java:92)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.app.Activity.performCreate(Activity.java:5231)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-16 04:09:56.641: E/AndroidRuntime(1230):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-16 04:09:56.641: E/AndroidRuntime(1230):     ... 11 more


这是否意味着我不能在setOnClickListener(new OnClick(){});使用数组引用?
如果可以的话,那么这个NPE来自哪里?

最佳答案

在很多情况下,mover [i] [j]为null

Button mover[][] = new Button[7][7];
int i = 0;
int j = 0;
for(i=0;i<7;i++)
{
    for(j=0;j<7;j++)
    {
        if(i<=1 && j<=1 || i>=6 && j<=1 || i<=1 && j>=6 || i>=6 && j>=6)
        {
            continue;
        }

        System.out.println("Setting mover["+i+"]["+j+"]");
    }
}


它给

Setting mover[0][2]
Setting mover[0][3]
Setting mover[0][4]
Setting mover[0][5]
Setting mover[1][2]
Setting mover[1][3]
Setting mover[1][4]
Setting mover[1][5]
Setting mover[2][0]
Setting mover[2][1]
Setting mover[2][2]
Setting mover[2][3]
Setting mover[2][4]
Setting mover[2][5]
Setting mover[2][6]
Setting mover[3][0]
Setting mover[3][1]
Setting mover[3][2]
Setting mover[3][3]
Setting mover[3][4]
Setting mover[3][5]
Setting mover[3][6]
Setting mover[4][0]
Setting mover[4][1]
Setting mover[4][2]
Setting mover[4][3]
Setting mover[4][4]
Setting mover[4][5]
Setting mover[4][6]
Setting mover[5][0]
Setting mover[5][1]
Setting mover[5][2]
Setting mover[5][3]
Setting mover[5][4]
Setting mover[5][5]
Setting mover[5][6]
Setting mover[6][2]
Setting mover[6][3]
Setting mover[6][4]
Setting mover[6][5]


但是,您只需要设置这3个

mover[0][2]=(Button)findViewById(R.id.mover13);
mover[0][3]=(Button)findViewById(R.id.mover14);
mover[0][4]=(Button)findViewById(R.id.mover15);


mover[0][5]发生时,它将引发NullPointerException

08-26 01:10