我得到一个NullPointerException

时间:2013-03-24 06:18:01

标签: android arrays nullpointerexception runtime

package As2.packageTK;



public class TicTacToeGame extends FragmentActivity{



    TextView p1Name;
    TextView p1Icon;
    TextView p2Name;
    TextView p2Icon;

    ArrayList<String> player1 = new ArrayList<String>();
    ArrayList<String> player2 = new ArrayList<String>();
    EditText row;
    EditText column;
    Button done;
    Button reset;

    //variables
    Random rand = new Random();

    int turn = rand.nextInt(2) + 1;

    Bundle extras = new Bundle();
    Bundle send = new Bundle();

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

        //gets id of each field
        p1Name = (TextView) this.findViewById(R.id.p1NameInfo);
        p1Icon = (TextView) this.findViewById(R.id.p1IconInfo);
        p2Name = (TextView) this.findViewById(R.id.p2NameInfo);
        p2Icon = (TextView) this.findViewById(R.id.p2IconInfo);
        done = (Button) this.findViewById(R.id.done);
        reset = (Button) this.findViewById(R.id.reset);
        row = (EditText) this.findViewById(R.id.row);
        column = (EditText) this.findViewById(R.id.column);



        //get all information from past activity
        extras = getIntent().getExtras(); 

        //settingd variables for incomcing values
        player1 = new ArrayList<String>(extras.getStringArrayList("player1"));
        player2 = new ArrayList<String>(extras.getStringArrayList("player2"));


        checkTurn(); //starts the game

        //buttons
        done.setOnClickListener(new View.OnClickListener() {            
            public void onClick(View v) {   
                if(checkField() == true)
                    checkTurn();
            }
        });

        reset.setOnClickListener(new View.OnClickListener() {           
            public void onClick(View v) {   
                //resets the game
            }
        });

    }


    public void setPlayer1()
    {
        int i = 0;
        String name = player1.get(i);
        if(name != null)
            p1Name.setText("Player Name: " + name);
        else 
            p1Name.setText("");

        i++;
        String icon = player1.get(i);

        if(icon != null)
            p1Icon.setText("Player Icon: " + icon);
        else
            p1Icon.setText("");
    }
    public void setPlayer2()
    {
        int i = 0;
        String name = player2.get(i);
        if(name == null) p2Name.setText("Player Name: " + name);
        else p2Name.setText("");

        i++;
        String icon = player2.get(i);

        if(icon == null) p2Icon.setText("Player Icon: " +icon);
        else p2Icon.setText("");
    }

    //checkTurn method
    public void checkTurn()
    {
        if(turn == 1)
        {
            changeLayout(turn);
            turn = 2;
        }
        else
        {
            changeLayout(turn);
            turn = 1;
        }
    }

    //changeLayout method
    public void changeLayout(int turn)
    {
        FragmentManager fm       = getSupportFragmentManager();
        Fragment        fragment = fm.findFragmentById(R.id.fragment_container);
        if(turn == 1) //if its player 1's turn
        {     
            if(fragment == null) //checks wether the current framelayout contains a fragment or not
            {
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.fragment_container, new PlayerTurn1());
                ft.commit(); 
                setPlayer1();
            }
        }
        else
        {           
            if(fragment == null)
            {
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.fragment_container, new PlayerTurn2());
                ft.commit(); 
                setPlayer2();
            }
        }
    }

    //checkField method
    public boolean checkField()
    {
        if(row == null || column == null)
        {
            Toast.makeText(this, "Please input the row or column!", Toast.LENGTH_LONG).show();
            return false;
        }
        else
            return true;
    }
}

好吧基本上我得到一个空指针异常。日志猫在下面。这段代码只是一个单独的类,用于使用一个帧布局和一个片段布局的井字游戏。 framelayout在彼此之间切换两个片段(每个片段代表一个播放器),另一个片段代表tic tac toe布局。

正如你所看到的,我从存储在arraylists中的过去活动中获取了玩家的信息

extras = getIntent().getExtras();   
player1 = new ArrayList<String>(extras.getStringArrayList("player1"));
player2 = new ArrayList<String>(extras.getStringArrayList("player2"));

这就是logcat输出的内容

  

03-24 05:47:14.585:E / AndroidRuntime(2998):致命异常:主要   03-24 05:47:14.585:E / AndroidRuntime(2998):   java.lang.RuntimeException:无法启动活动   ComponentInfo {As2.packageTK / As2.packageTK.TicTacToeGame}:   java.lang.NullPointerException 03-24 05:47:14.585:   E / AndroidRuntime(2998):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)   03-24 05:47:14.585:E / AndroidRuntime(2998):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)   03-24 05:47:14.585:E / AndroidRuntime(2998):at   android.app.ActivityThread.access $ 600(ActivityThread.java:141)03-24   05:47:14.585:E / AndroidRuntime(2998):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)   03-24 05:47:14.585:E / AndroidRuntime(2998):at   android.os.Handler.dispatchMessage(Handler.java:99)03-24   05:47:14.585:E / AndroidRuntime(2998):at   android.os.Looper.loop(Looper.java:137)03-24 05:47:14.585:   E / AndroidRuntime(2998):at   android.app.ActivityThread.main(ActivityThread.java:5039)03-24   05:47:14.585:E / AndroidRuntime(2998):at   java.lang.reflect.Method.invokeNative(Native Method)03-24   05:47:14.585:E / AndroidRuntime(2998):at   java.lang.reflect.Method.invoke(Method.java:511)03-24 05:47:14.585:   E / AndroidRuntime(2998):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)   03-24 05:47:14.585:E / AndroidRuntime(2998):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)03-24   05:47:14.585:E / AndroidRuntime(2998):at   dalvik.system.NativeStart.main(Native Method)03-24 05:47:14.585:   E / AndroidRuntime(2998):引起:java.lang.NullPointerException   03-24 05:47:14.585:E / AndroidRuntime(2998):at   As2.packageTK.TicTacToeGame.setPlayer1(TicTacToeGame.java:107)03-24   05:47:14.585:E / AndroidRuntime(2998):at   As2.packageTK.TicTacToeGame.changeLayout(TicTacToeGame.java:160)03-24   05:47:14.585:E / AndroidRuntime(2998):at   As2.packageTK.TicTacToeGame.checkTurn(TicTacToeGame.java:138)03-24   05:47:14.585:E / AndroidRuntime(2998):at   As2.packageTK.TicTacToeGame.onCreate(TicTacToeGame.java:83)03-24   05:47:14.585:E / AndroidRuntime(2998):at   android.app.Activity.performCreate(Activity.java:5104)03-24   05:47:14.585:E / AndroidRuntime(2998):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)   03-24 05:47:14.585:E / AndroidRuntime(2998):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)   03-24 05:47:14.585:E / AndroidRuntime(2998):... 11更多

这是错误显然位于

的地方
public void setPlayer1()
{
    int i = 0;
    String name = player1.get(i);
    if(name != null)
        p1Name.setText("Player Name: " + name);
    else 
        p1Name.setText("");

    i++;
    String icon = player1.get(i);

    if(icon != null)
        p1Icon.setText("Player Icon: " + icon);
    else
        p1Icon.setText("");
}

特别是在这一行

if(icon != null)

如果没有正确详细说明,我很抱歉。如果有一个没有任何意义的逻辑告诉我,因为我还在努力,我只是试图通过这个nullpointer。

0 个答案:

没有答案