0

class GamePlay was work well untill I convert Activity to Fragment.

The button not working in layout fragment_game.xml android:onClick="newGame" not perform action on click.

I dont want to add Listner.

Many Thanks for any help:-)

public void newGame(View view) {
        noughtsTurn = false;
        board = new char[3][3];
        resetButtons();
    }

class GamePlay:

public class GamePlay extends Fragment {

    // Representing the game state:
    private boolean noughtsTurn = false; // Who's turn is it? false=X true=O
    private char board[][]      = new char[3][3]; // for now we will represent the board as an array of characters

    private LinearLayout          lLayout     = null;
    private FragmentActivity      faActivity  = null;
    /**
    * Called when the activity is first created.
    */

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        faActivity  = (FragmentActivity)    super.getActivity();    
        lLayout    = (LinearLayout)    inflater.inflate(R.layout.fragment_game, container, false);


            setupOnClickListeners();
            resetButtons();
            return lLayout;
    }

    /**
     * Called when you press new game.
     *
     * @param view the New Game Button
     */
    public void newGame(View view) {
        noughtsTurn = false;
        board = new char[3][3];
        resetButtons();
    }

    /**
     * Reset each button in the grid to be blank and enabled.
     */
    private void resetButtons() {
        TableLayout T = (TableLayout) lLayout.findViewById(R.id.tableLayout);
        for (int y = 0; y < T.getChildCount(); y++) {
            if (T.getChildAt(y) instanceof TableRow) {
                TableRow R = (TableRow) T.getChildAt(y);
                for (int x = 0; x < R.getChildCount(); x++) {
                    if (R.getChildAt(x) instanceof Button) {
                        Button B = (Button) R.getChildAt(x);
                        B.setText("");
                        B.setEnabled(true);
                    }
                }
            }
        }
}

fragment_game.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title"
            android:id="@+id/titleText" 
            android:layout_gravity="center_horizontal"/>
    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Game"
            android:id="@+id/newGameBtn" 
            android:layout_column="0" 
            android:layout_gravity="center_horizontal"
            android:onClick="newGame"/>
    <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:id="@+id/tableLayout">
        <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="O"
                    android:textSize="100dp"
                    android:id="@+id/topLeft" android:layout_column="1" android:width="100dp" android:height="150dp"/>
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:textSize="100dp"
                    android:id="@+id/button" android:layout_column="2" android:width="100dp" android:height="150dp"/>
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:textSize="100dp"
                    android:id="@+id/button1" android:layout_column="3" android:width="100dp" android:height="150dp"/>
        </TableRow>
        <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:textSize="100dp"
                    android:id="@+id/button2" android:layout_column="1" android:width="100dp" android:height="150dp"/>
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:textSize="100dp"
                    android:id="@+id/button3" android:layout_column="2" android:width="100dp" android:height="150dp"/>
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:textSize="100dp"
                    android:id="@+id/button4" android:layout_column="3" android:width="100dp" android:height="150dp"/>
        </TableRow>
        <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:textSize="100dp"
                    android:id="@+id/button5" android:layout_column="1" android:width="100dp" android:height="150dp"/>
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:textSize="100dp"
                    android:id="@+id/button6" android:layout_column="2" android:width="100dp" android:height="150dp"/>
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="O"
                    android:textSize="100dp"
                    android:id="@+id/button7" android:layout_column="3" android:width="100dp" android:height="150dp"/>
        </TableRow>
    </TableLayout>
</LinearLayout>
bigfood
  • 13
  • 4

1 Answers1

0

If you don't want to manually add the listener, then you must move your onClick method newGame into the Activity that your fragment is attached to.

Otherwise, you must programmatically set the OnClickListener in GamePlay (check out this answer for a clever way of doing this)

Community
  • 1
  • 1
bmat
  • 2,084
  • 18
  • 24