我正在尝试编写一个小小的tictactoe游戏。现在我到达了一个抛出nullPointerException的地步,我无法解释。有人可以帮忙吗?

代码主要 Activity :

    package de.android.pali;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class TicTacToeMenu extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tic_tac_toe_menu);

    final Button createGame = (Button)findViewById(R.id.newGameButton);

    final Intent newGame = new Intent(TicTacToeMenu.this, newGame.class);

    createGame.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            startActivity(newGame);


        }
    });


}


}

第二 Activity 代码:
package de.android.pali;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class newGame extends TicTacToeMenu{

int turn = 1;
int[] felder = new int[9];
TextView ergebnis = (TextView)findViewById(R.id.winners);

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    final ImageButton feld1 = (ImageButton)findViewById(R.id.feld1);
    final ImageButton feld2 = (ImageButton)findViewById(R.id.feld2);
    final ImageButton feld3 = (ImageButton)findViewById(R.id.feld3);
    final ImageButton feld4 = (ImageButton)findViewById(R.id.feld4);
    final ImageButton feld5 = (ImageButton)findViewById(R.id.feld5);
    final ImageButton feld6 = (ImageButton)findViewById(R.id.feld6);
    final ImageButton feld7 = (ImageButton)findViewById(R.id.feld7);
    final ImageButton feld8 = (ImageButton)findViewById(R.id.feld8);
    final ImageButton feld9 = (ImageButton)findViewById(R.id.feld9);




    feld1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            if(turn==1){
            feld1.setBackgroundResource(R.drawable.kreis);
            turn = 0;
            felder[0] = 1;
            }
            else{
                feld1.setBackgroundResource(R.drawable.kreuz);
                turn = 1;
                felder[0] = 0;
            }
        }


    });

    feld2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(turn==1){
                feld2.setBackgroundResource(R.drawable.kreis);
                turn = 0;
                felder[1] = 1;
                }
                else{
                        feld2.setBackgroundResource(R.drawable.kreuz);
                    felder[1] = 0;
                }
        }
    });

    feld3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(turn==1){
                feld3.setBackgroundResource(R.drawable.kreis);
                turn = 0;
                felder[2] = 1;
                }
                else{
                    feld3.setBackgroundResource(R.drawable.kreuz);
                    turn = 1;
                    felder[2] = 0;
                };

        }
    });

    feld4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(turn==1){
                feld4.setBackgroundResource(R.drawable.kreis);
                turn = 0;
                felder[3] = 1;
                }
                else{
                    feld4.setBackgroundResource(R.drawable.kreuz);
                    felder[3] = 0;
                }

        }
    });

    feld5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(turn==1){
                feld5.setBackgroundResource(R.drawable.kreis);
                turn = 0;
                felder[4] = 1;
                }
                else{
                    feld5.setBackgroundResource(R.drawable.kreuz);
                    turn = 1;
                    felder[4] = 0;
                }

        }
    });

    feld6.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(turn==1){
                feld6.setBackgroundResource(R.drawable.kreis);
                turn = 0;
                felder[5] = 1;
                }
                else{
                    feld6.setBackgroundResource(R.drawable.kreuz);
                    turn = 1;
                    felder[5] = 0;
                }

        }
    });

    feld7.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(turn==1){
                feld7.setBackgroundResource(R.drawable.kreis);
                turn = 0;
                felder[6] = 1;
                }
                else{
                    feld7.setBackgroundResource(R.drawable.kreuz);
                    turn = 1;
                    felder[6] = 0;
                }

        }
    });

    feld8.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(turn==1){
                feld8.setBackgroundResource(R.drawable.kreis);
                turn = 0;
                felder[7] = 1;
                }
                else{
                    feld8.setBackgroundResource(R.drawable.kreuz);
                    turn = 1;
                    felder[7] = 0;
                }

        }
    });

    feld9.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(turn==1){
                feld9.setBackgroundResource(R.drawable.kreis);
                turn = 0;
                felder[8] = 1;
                }
                else{
                    feld9.setBackgroundResource(R.drawable.kreuz);
                    turn = 1;
                    felder[8] = 0;
                }

        }
    });


}

public int getTurn() {
    int whois = turn;
    return whois;
}

public void checkWinner(){
    if(felder[0]==1 & felder[1]==1 & felder[2]==1){
        ergebnis.setText(R.string.kreuz);

    }else
    { }

}
}

Logcat:
    11-13 12:26:55.579: I/ActivityManager(88): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=de.android.pali/.TicTacToeMenu} from pid 772
11-13 12:26:55.589: W/WindowManager(88): Failure taking screenshot for (180x300) to layer 21005
11-13 12:26:55.629: D/AndroidRuntime(772): Shutting down VM
11-13 12:26:55.639: D/dalvikvm(772): GC_CONCURRENT freed 101K, 77% free 485K/2048K, paused 1ms+1ms
11-13 12:26:55.649: D/dalvikvm(772): Debugger has detached; object registry had 1 entries
11-13 12:26:55.659: D/dalvikvm(782): Not late-enabling CheckJNI (already on)
11-13 12:26:55.669: I/AndroidRuntime(772): NOTE: attach of thread 'Binder Thread #3' failed
11-13 12:26:55.719: I/ActivityManager(88): Start proc de.android.pali for activity de.android.pali/.TicTacToeMenu: pid=782 uid=10045 gids={}
11-13 12:26:55.948: W/NetworkManagementSocketTagger(88): setKernelCountSet(10045, 1) failed with errno -2
11-13 12:26:56.449: I/Process(88): Sending signal. PID: 782 SIG: 3
11-13 12:26:56.449: I/dalvikvm(782): threadid=3: reacting to signal 3
11-13 12:26:56.569: I/dalvikvm(782): Wrote stack traces to '/data/anr/traces.txt'
11-13 12:26:56.819: D/gralloc_goldfish(782): Emulator without GPU emulation detected.
11-13 12:26:56.868: I/ActivityManager(88): Displayed de.android.pali/.TicTacToeMenu: +1s263ms (total +37s914ms)
11-13 12:26:57.088: W/NetworkManagementSocketTagger(88): setKernelCountSet(10009, 0) failed with errno -2
11-13 12:27:00.828: I/ActivityManager(88): START {cmp=de.android.pali/.newGame} from pid 782
11-13 12:27:00.838: W/WindowManager(88): Failure taking screenshot for (180x300) to layer 21010
11-13 12:27:00.868: D/AndroidRuntime(782): Shutting down VM
11-13 12:27:00.868: W/dalvikvm(782): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-13 12:27:00.889: E/AndroidRuntime(782): FATAL EXCEPTION: main
11-13 12:27:00.889: E/AndroidRuntime(782): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{de.android.pali/de.android.pali.newGame}: java.lang.NullPointerException
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.os.Looper.loop(Looper.java:137)
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.app.ActivityThread.main(ActivityThread.java:4424)
11-13 12:27:00.889: E/AndroidRuntime(782):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 12:27:00.889: E/AndroidRuntime(782):  at java.lang.reflect.Method.invoke(Method.java:511)
11-13 12:27:00.889: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-13 12:27:00.889: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-13 12:27:00.889: E/AndroidRuntime(782):  at dalvik.system.NativeStart.main(Native Method)
11-13 12:27:00.889: E/AndroidRuntime(782): Caused by: java.lang.NullPointerException
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.app.Activity.findViewById(Activity.java:1794)
11-13 12:27:00.889: E/AndroidRuntime(782):  at de.android.pali.newGame.<init>(newGame.java:13)
11-13 12:27:00.889: E/AndroidRuntime(782):  at java.lang.Class.newInstanceImpl(Native Method)
11-13 12:27:00.889: E/AndroidRuntime(782):  at java.lang.Class.newInstance(Class.java:1319)
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-13 12:27:00.889: E/AndroidRuntime(782):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
11-13 12:27:00.889: E/AndroidRuntime(782):  ... 11 more
11-13 12:27:00.929: W/ActivityManager(88):   Force finishing activity de.android.pali/.newGame
11-13 12:27:00.929: W/ActivityManager(88):   Force finishing activity de.android.pali/.TicTacToeMenu
11-13 12:27:01.359: I/Process(88): Sending signal. PID: 782 SIG: 3
11-13 12:27:01.359: I/dalvikvm(782): threadid=3: reacting to signal 3
11-13 12:27:01.379: I/dalvikvm(782): Wrote stack traces to '/data/anr/traces.txt'
11-13 12:27:01.439: W/ActivityManager(88): Activity pause timeout for ActivityRecord{415b08b0 de.android.pali/.newGame}
11-13 12:27:01.439: I/Process(88): Sending signal. PID: 782 SIG: 3
11-13 12:27:01.439: I/dalvikvm(782): threadid=3: reacting to signal 3
11-13 12:27:01.459: I/dalvikvm(782): Wrote stack traces to '/data/anr/traces.txt'
11-13 12:27:01.488: W/NetworkManagementSocketTagger(88): setKernelCountSet(10009, 1) failed with errno -2
11-13 12:27:01.748: W/NetworkManagementSocketTagger(88): setKernelCountSet(10045, 0) failed with errno -2
11-13 12:27:02.169: I/Process(782): Sending signal. PID: 782 SIG: 9
11-13 12:27:02.178: I/ActivityManager(88): Process de.android.pali (pid 782) has died.
11-13 12:27:02.178: W/InputDispatcher(88): channel '415c53c8 de.android.pali/de.android.pali.TicTacToeMenu (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
11-13 12:27:02.178: E/InputDispatcher(88): channel '415c53c8 de.android.pali/de.android.pali.TicTacToeMenu (server)' ~ Channel is unrecoverably broken and will be disposed!
11-13 12:27:02.189: I/WindowManager(88): WIN DEATH: Window{415c53c8 de.android.pali/de.android.pali.TicTacToeMenu paused=true}
11-13 12:27:02.189: W/InputDispatcher(88): Attempted to unregister already unregistered input channel '415c53c8 de.android.pali/de.android.pali.TicTacToeMenu (server)'
11-13 12:27:02.199: I/WindowManager(88): WINDOW DIED Window{415c53c8 de.android.pali/de.android.pali.TicTacToeMenu paused=true}
11-13 12:27:02.359: W/InputManagerService(88): Got RemoteException sending setActive(false) notification to pid 782 uid 10045
11-13 12:27:11.749: W/ActivityManager(88): Activity destroy timeout for  ActivityRecord{415a28b8 de.android.pali/.TicTacToeMenu}
11-13 12:27:11.749: W/ActivityManager(88): Activity destroy timeout for ActivityRecord{415b08b0 de.android.pali/.newGame}

我希望任何人都可以帮助:)

最佳答案

logcat的这一行显示错误在代码中的何处:

11-13 12:27:00.889: E/AndroidRuntime(782):  at de.android.pali.newGame.<init>(newGame.java:13)

这似乎与此有关:
feld1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
....

由于某种原因,当您调用feld1时找不到final ImageButton feld1 = (ImageButton)findViewById(R.id.feld1);。您确定这是该布局文件(游戏)上ImageButton的正确ID吗?

10-08 13:46