2013-03-27 107 views
1

我想调用另一个片段的方法,但它不断告诉我,我的TextViews的值为空..?如何从另一个片段类访问一个方法?

PLAYER1:

public class PlayerTurn1 extends Fragment { 

    TextView p1Name; 
    TextView p1Icon; 
    Button doneP1; 
    Button resetP1; 
    EditText row; 
    EditText column; 
    TicTacToeLayout myObject = new TicTacToeLayout(); 
    ArrayList<String> player1; 
    Bundle extras = new Bundle(); 
    int turn = 1; 

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

     extras = getArguments(); 

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

     Toast.makeText(getActivity(), player1.get(0), Toast.LENGTH_LONG).show();//Name of the player 
     Toast.makeText(getActivity(), player1.get(1), Toast.LENGTH_LONG).show();//Icon chosen by the player 

     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) 
       { 
        int rowValueInt = Integer.parseInt(row.getText().toString()); 
        int colValueInt = Integer.parseInt(column.getText().toString()); 

        myObject.play(rowValueInt, colValueInt, player1.get(1)); 
        callPlayer2Fragment(); 

       } 


      } 
     }); 
    } 

玩家2片段是完全一样的代码,所以我不会打扰添加。

TicTacToeLayout片段类:

public class TicTacToeLayout extends Fragment { 

    TextView image1, image2, image3, image4, image5, image6, image7, image8, image9; 
    TextView[][] images; 


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

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


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

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

     toast(); 
    } 

    public void toast() 
    { 
     Toast.makeText(getActivity(), images[0][0].getText().toString(), Toast.LENGTH_LONG).show(); 
     Toast.makeText(getActivity(), images[0][1].getText().toString(), Toast.LENGTH_LONG).show(); 
     Toast.makeText(getActivity(), images[0][2].getText().toString(), Toast.LENGTH_LONG).show(); 
     Toast.makeText(getActivity(), images[1][0].getText().toString(), Toast.LENGTH_LONG).show(); 
    } 

    public void play(int row, int column, String icon) 
    { 
      images[row-1][column-1].setText(icon); 
    } 

} 

我只是做了尝试和烤面包片只是为了确保有东西在数组中和它的工作方法。但是,只有在创建片段时才会提供一些东西。不知何故,在方法调用myObject.play(rowValueInt, colValueInt, player1.get(1)); 后,调试器告诉我,我的图像的所有值都为空。

这是调试器这样说:

this TicTacToeLayout (id=830016401072) 
    image1 null  
    image2 null  
    image3 null  
    image4 null  
    image5 null  
    image6 null  
    image7 null  
    image8 null  
    image9 null  
    images null  

row 1 
column 2 
icon "O" (id=830016398920) 

当我点击完成按钮这基本上发生。应该发生的事情是当玩家点击按钮时,TicTacToeLayout片段中的文本视图位于玩家输入的任何行和列改变为X或O.行,列和图标都有它们各自的值,但是在对象调用后图像结束为空。有任何想法吗?我的线程已关闭,因为显然我没有提供太多的信息,管理员生气,所以如果你需要任何东西,只要告诉我,我会更新。位于PlayerTurn1

点击完成按钮时
public void callPlayer2Fragment() 
    { 
     FragmentManager fm  = getFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 
     Fragment Player2Frag = new PlayerTurn2(); 
     Player2Frag.setArguments(extras); 
     ft.replace(R.id.fragment_container, Player2Frag); 
     ft.commit(); 
    } 

第二个玩家片段

编辑 调用,PlayerTurn2片段将被调用并且从这个类的论点,但这个片段被替换。

XML布局

<?xml version="1.0" encoding="UTF-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" > 

     <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="0px" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

     <fragment 
      android:id="@+id/frag2" 
      android:name="As2.packageTK.TicTacToeLayout" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

    </LinearLayout> 

回答

0

原因他们都是空的是因为片段不是“活着”(即附加或创建)。您应该通过活动中的界面来完成。我解释它here

+0

我会给它另一个镜头 – Morpheglus 2013-03-27 06:22:54

+0

我刚刚看到你对其他建议的答案的评论。如果当前未连接,则无法更新片段。当你再次附加它时(通过将它放在FrameLayout或其他东西中),将相关的更新信息传递给你的Fragment。 – 2013-03-27 06:27:06

+0

嗯,这就是我在做什么。我会更新并向您显示我的XML并致电给它。查看我的新更新问题 – Morpheglus 2013-03-27 06:30:43