1

I wish to call a method from another fragment but it keeps telling me the values of my TextViews are null..?

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

                }


            }
        });
    }

player 2 fragment is exactly the same code so I won't bother adding it.

TicTacToeLayout fragment class:

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

}

I just made a method to try and toast just to be sure that there is something in the array and it worked. However, it toast something only when the fragment is created. Somehow after the method call myObject.play(rowValueInt, colValueInt, player1.get(1)); the debugger tells me that all the values of my images are null.

This is what the debugger says:

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)   

This basically happens when I click the done button. What is supposed to happen is when the player hits the button, in the TicTacToeLayout fragment, the textview located at whatever row and column the player inputed changes to the X or O. the row, column and icon all have their respective value but the images ended up null after the call of the object. any ideas? My thread's been closed because apparently I didn't provide much information and the admins got pissed so if you need anything else just tell me I'll update.

EDIT call of the second player fragment located in 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();
    }

when the done button is clicked, the PlayerTurn2 fragment is called and passed the arguments from this class but this fragment is replaced.

XML layout

<?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>
Morpheglus
  • 11
  • 1
  • 6

1 Answers1

0

The reason they're all null is because the Fragment isn't currently "alive" (i.e. attached or created). You should do it via an interface in the Activity. I explain it here

Community
  • 1
  • 1
Mike T
  • 4,747
  • 4
  • 32
  • 52
  • I just saw your comment on the other suggested answer. You can't update a Fragment if it's not currently attached. Rather pass your Fragment the relevant update information when you attach it again (by putting it in a FrameLayout or something). – Mike T Mar 27 '13 at 06:27
  • Well ya thats what I'm doing. I'll update and show you my XML and call to it. See my new updated question – Morpheglus Mar 27 '13 at 06:30
  • I just found my solution, I think... Instead of having the host activity change the fragment layout when the player is done with his turn, I'll just have the PlayerTurn1 and PlayerTurn2 fragments do it(bascially what I have just posted) but sending their data through the host activity instead all the way to the TicTacToeLayout, is that good? – Morpheglus Mar 27 '13 at 06:43
  • I'm not 100% sure which fragment is being replaced. But, if you want to update the Fragment being shown, just updated it (because you can). If you want to update the Fragment that is about to be displayed (the one you're switching to), then pass the information you need in `extras` when you call ` Player2Frag.setArguments(extras);`. Then use that information to do the updates you require – Mike T Mar 27 '13 at 06:48
  • Maybe my explanation wasn't clear. I'm not trying to update the information in the fragments I'm inter=switching. The information I want to update is in the second fragment given in the XML layout. That fragment is the layout of the tic tac toe game. What I thought is that instead of updating data between fragments directly (PlayerTurn1 to TicTacToeLayout), I could refer to a method call in the host activity and then have that method update the information in the TicTacToeLayout. Is that good? – Morpheglus Mar 27 '13 at 08:22
  • Yes, that's exactly what you should do :) – Mike T Mar 27 '13 at 08:49
  • I don't know how to do it actually. I want to pass intent from a fragment to a host activity without starting the activity, is this even possible? I think it's called dynamic update, not sure. – Morpheglus Mar 27 '13 at 13:34
  • Let's chat about it when you have time. Start a chat with me and I'll tell you more. I'm pretty sure doing this via Intents is not really what you want. I just realised you only have 2 reputation so you can't chat. The last paragraph of my answer in the link explains how I think you should be doing it – Mike T Mar 28 '13 at 05:44