0

我只想感谢昨天回答我帖子的人,这对我帮助很大,但现在话题不同了。我设法修复了 NPE,但显然我的活动正在被破坏。

这是日志:

03-26 06:55:27.411: E/AndroidRuntime(1321): java.lang.IllegalStateException: Activity has been destroyed
03-26 06:55:27.411: E/AndroidRuntime(1321):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342)
03-26 06:55:27.411: E/AndroidRuntime(1321):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
03-26 06:55:27.411: E/AndroidRuntime(1321):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
03-26 06:55:27.411: E/AndroidRuntime(1321):     at As2.packageTK.TicTacToeGame.changeLayout(TicTacToeGame.java:157)
03-26 06:55:27.411: E/AndroidRuntime(1321):     at As2.packageTK.TicTacToeGame.checkTurn(TicTacToeGame.java:132)
03-26 06:55:27.411: E/AndroidRuntime(1321):     at As2.packageTK.PlayerTurn1$1.onClick(PlayerTurn1.java:34)
03-26 06:55:27.411: E/AndroidRuntime(1321):     at android.view.View.performClick(View.java:4202)

这些是位于我的 TicTacToeGame FragmentActivity 类中的 checkTurn() 和 changeLayout():

public class TicTacToeGame extends FragmentActivity{

    //fields..
    public int checkTurn()
    {
        if(turn == 1)
        {
            changeLayout(turn);
            turn = 2;
            return 1;
        }
        else
        {
            changeLayout(turn);
            turn = 1;
            return 2;
        }
    }

    //changeLayout method
    public void changeLayout(int turn)
    {
        FragmentManager fm       = getSupportFragmentManager();
        Fragment        fragment = fm.findFragmentById(R.id.fragment_container);
        FragmentTransaction ft;

        if(turn == 1) //if its player 1's turn
        {  
            if(fragment == null) //checks wether the current framelayout contains a fragment or not
            {   
                ft = fm.beginTransaction();
                ft.add(R.id.fragment_container, new PlayerTurn1());
                ft.commit(); 
                //setPlayer1();
            }
            else
            {
                ft = fm.beginTransaction();
                ft.replace(R.id.fragment_container, new PlayerTurn1());
                ft.commit();
                //setPlayer1();

            }
        }
        else
        {   
            if(fragment == null)
            {
                ft = fm.beginTransaction();;
                ft.add(R.id.fragment_container, new PlayerTurn2());
                ft.commit(); 
                //setPlayer2();
            }
            else
            {
                ft = fm.beginTransaction();;
                ft.replace(R.id.fragment_container, new PlayerTurn2());
                ft.commit(); 
                //setPlayer2();
            }
        }
    }
}

这些是我的片段的 onCreatedActivity 和 onCreateView:

 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);

            doneP1 = (Button) getActivity().findViewById(R.id.doneP1);
            //resetP1 = (Button) this.getActivity().findViewById(R.id.resetP2);

            doneP1.setOnClickListener(new View.OnClickListener() {          
                public void onClick(View v) {   
                    object.checkTurn();
                }
            });
        }

有任何想法吗?

4

0 回答 0