另一个NPE

时间:2013-03-26 15:26:39

标签: java android fragment

我有3个片段,2个是player1和player2,还有一个是tictactoelayout,其中一个基本上将所有片段连接在一起,即TicTacToeGame。在我的每个播放器片段中,我正在尝试访问TicTacToeGame方法

public class PlayerTurn1 extends Fragment  {

public View onCreateView(LayoutInflater inflater,
    ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.playerturn1, container, false);
        return view;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

       extras = getArguments();

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

        row = (EditText) getActivity().findViewById(R.id.rowP1);
        column = (EditText) getActivity().findViewById(R.id.columnP1);
        p1Name = (TextView) getActivity().findViewById(R.id.p1NameInfo);
        p1Icon = (TextView) getActivity().findViewById(R.id.p1IconInfo);
        doneP1 = (Button) getActivity().findViewById(R.id.doneP1);
        resetP1 = (Button) getActivity().findViewById(R.id.resetP2);

        setPlayer();

        doneP1.setOnClickListener(new View.OnClickListener() {          
            public void onClick(View v) {   

                if(checkField() != false)
                {
                    String rowValueStr = row.getText().toString();
                    String colValueStr = column.getText().toString();
                    int rowValueInt = Integer.parseInt(rowValueStr);
                    int colValueInt = Integer.parseInt(colValueStr);

                    myObject.playTicTacToe(rowValueInt, colValueInt);
                    myObject.whosTurn = 2;
                    callPlayer2Fragment();

                }


            }
        });
    }
}

这是我的Player2片段:

public class PlayerTurn2 extends Fragment{
 public View onCreateView(LayoutInflater inflater,
    ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.playerturn2, container, false);

        return view;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        extras = getArguments();

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

        p2Name = (TextView) getActivity().findViewById(R.id.p2NameInfo);
        p2Icon = (TextView) getActivity().findViewById(R.id.p2IconInfo);
        row = (EditText) getActivity().findViewById(R.id.rowP2);
        column = (EditText) getActivity().findViewById(R.id.columnP2);
        doneP2 = (Button) getActivity().findViewById(R.id.doneP2);
        resetP2 = (Button) getActivity().findViewById(R.id.resetP2);

        setPlayer();

        doneP2.setOnClickListener(new View.OnClickListener() {          
            public void onClick(View v) {   

                if(checkField() != false)
                {
                    String rowValueStr = row.getText().toString();
                    String colValueStr = column.getText().toString();
                    int rowValueInt = Integer.parseInt(rowValueStr);
                    int colValueInt = Integer.parseInt(colValueStr);

                    myObject2.playTicTacToe(rowValueInt, colValueInt);
                    myObject2.whosTurn = 1;
                    callPlayer1Fragment();
                }
            }
        });
    }
}

这是我在TicTacToeGame中的方法:

public void playTicTacToe(int r, int c)
    {
        if(whosTurn == 1)
            images[r-1][c-1].setText(player1.get(1));  
        else
            images[r-1][c-1].setText(player2.get(1));
        return;

    }

这是日志猫:

03-26 15:11:31.770: E/AndroidRuntime(5632): java.lang.NullPointerException
03-26 15:11:31.770: E/AndroidRuntime(5632):     at As2.packageTK.TicTacToeGame.playTicTacToe(TicTacToeGame.java:153)
03-26 15:11:31.770: E/AndroidRuntime(5632):     at As2.packageTK.PlayerTurn1$1.onClick(PlayerTurn1.java:64)

第64行:

myObject.playTicTacToe(rowValueInt, colValueInt);

第153行:

images[r-1][c-1].setText("O");

有任何想法如何解决这个问题?

修改

    TextView[][] images;
image1 = (TextView) this.findViewById(R.id.Image1);
        image2 = (TextView) this.findViewById(R.id.Image2);
        image3 = (TextView) this.findViewById(R.id.Image3);
        image4 = (TextView) this.findViewById(R.id.Image4);
        image5 = (TextView) this.findViewById(R.id.Image5);
        image6 = (TextView) this.findViewById(R.id.Image6);
        image7 = (TextView) this.findViewById(R.id.Image7);
        image8 = (TextView) this.findViewById(R.id.Image8);
        image9 = (TextView) this.findViewById(R.id.Image9);

    images = new TextView[][]{ {image1, image2, image3},
                       {image4, image5, image6},
                       {image7, image8, image9} }

2 个答案:

答案 0 :(得分:3)

  

有任何想法如何解决这个问题?

是。仔细分析代码。

你说NPE在这个陈述中被抛出。

images[r-1][c-1].setText("O");
  • rc都是int,因此不能null
  • images数组可以null
  • images[r-1]子阵列可以null
  • images[r-1][c-1]对象可以null

(如果不是抛出异常的地方,那么对抛出NPE的实际代码进行类似的分析。)

因此,您可能有3种可能的NPE直接原因。使用调试器或跟踪打印来确定以上哪个实际上是null。然后查看创建,初始化和更新数组的代码,以确定这些null值的来源。然后解决问题。

我无法帮助您找到空值的来源,因为相关代码未包含在问题中。

答案 1 :(得分:1)

来自LogCat:

java.lang.NullPointerException
    at As2.packageTK.TicTacToeGame.playTicTacToe(TicTacToeGame.java:153)

您可以在playTicTacToe()中看到问题(特别是在第153行,但我无法确定这是哪一行)。如果这是完整的方法:

public void playTicTacToe(int r, int c)
{
    if(whosTurn == 1)
        images[r-1][c-1].setText(player1.get(1));  
    else
        images[r-1][c-1].setText(player2.get(1));
    return;
}

然后images[r-1][c-1]player1和/或player2为空...您的调试器将帮助您比我们更快地找出哪些值为null,然后简单地初始化变量(多个)。
NPE并不是那么糟糕。