2

in if(saveInstanceState != null) I am trying to save the changes of the text in Text Views that happened by whoWon() method to save it in while user rotate the phone and get it back as it was before rotation but it shows me an error that says getText() in TextView cannot be applied to (java.lang.string) , I tried to add .toString() but it didn't work.

public class Multiplayer extends AppCompatActivity {
    private TextView mTicTacTextViews [] = new TextView[9];
    private Button mButtonX;
    private Button mButtonO;
    private Button mResult;
    private int mcheckAllButtonsClicked =0;
    private String TEXT_VIEW_VALUES_KEY = "VALUES OF TEXT VIEW'S";
    private String mTextviewValues [] = new String[9];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null)
        {
            String values[]=savedInstanceState.getStringArray(TEXT_VIEW_VALUES_KEY);
            for (int i=0;i<values.length;i++ ) {
                mTextviewValues[i]=values[i];
                mTicTacTextViews[i].getText(mTextviewValues[i]);
            }

        }

        setContentView(R.layout.activity_tic_tac);

        mTicTacTextViews[0] = (TextView) findViewById(R.id.tic_tac_one);
        mTicTacTextViews[0].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               ifPressed_XButton(mTicTacTextViews[0]);
                ifPressed_OButton(mTicTacTextViews[0]);
                mcheckAllButtonsClicked+=1;
            }
        });

        mTicTacTextViews[1] = (TextView) findViewById(R.id.tic_tac_two);
        mTicTacTextViews[1].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ifPressed_XButton(mTicTacTextViews[1]);
                ifPressed_OButton(mTicTacTextViews[1]);
                mcheckAllButtonsClicked+=1;
            }
        });

        mTicTacTextViews[2] = (TextView) findViewById(R.id.tic_tac_three);
        mTicTacTextViews[2].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ifPressed_XButton(mTicTacTextViews[2]);
                ifPressed_OButton(mTicTacTextViews[2]);
                mcheckAllButtonsClicked+=1;
            }
        });

        mTicTacTextViews[3] = (TextView) findViewById(R.id.tic_tac_four);
        mTicTacTextViews[3].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ifPressed_XButton(mTicTacTextViews[3]);
                ifPressed_OButton(mTicTacTextViews[3]);
                mcheckAllButtonsClicked+=1;
            }
        });
        mTicTacTextViews[4] = (TextView) findViewById(R.id.tic_tac_five);
        mTicTacTextViews[4].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ifPressed_XButton(mTicTacTextViews[4]);
                ifPressed_OButton(mTicTacTextViews[4]);
                mcheckAllButtonsClicked+=1;
            }
        });

        mTicTacTextViews[5] = (TextView) findViewById(R.id.tic_tac_six);
        mTicTacTextViews[5].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ifPressed_XButton(mTicTacTextViews[5]);
                ifPressed_OButton(mTicTacTextViews[5]);
                mcheckAllButtonsClicked+=1;
            }
        });

        mTicTacTextViews[6] = (TextView) findViewById(R.id.tic_tac_seven);
        mTicTacTextViews[6].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ifPressed_XButton(mTicTacTextViews[6]);
                ifPressed_OButton(mTicTacTextViews[6]);
                mcheckAllButtonsClicked+=1;
            }
        });

        mTicTacTextViews[7] = (TextView) findViewById(R.id.tic_tac_eight);
        mTicTacTextViews[7].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ifPressed_XButton(mTicTacTextViews[7]);
                ifPressed_OButton(mTicTacTextViews[7]);
                mcheckAllButtonsClicked+=1;
            }
        });

        mTicTacTextViews[8] = (TextView) findViewById(R.id.tic_tac_nine);
        mTicTacTextViews[8].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ifPressed_XButton(mTicTacTextViews[8]);
                ifPressed_OButton(mTicTacTextViews[8]);
                mcheckAllButtonsClicked+=1;

            }
        });



        mButtonX = (Button) findViewById(R.id.x_button);
        mButtonX.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUserPressed_XButton =true;
            }
        });

        mButtonO = (Button) findViewById(R.id.o_button);
        mButtonO.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUserPressed_OButton =true;

            }
        });

        mTextviewValues [0]=mTicTacTextViews[0].getText().toString();
        mTextviewValues [1]=mTicTacTextViews[1].getText().toString();
        mTextviewValues [2]=mTicTacTextViews[2].getText().toString();
        mTextviewValues [3]=mTicTacTextViews[3].getText().toString();
        mTextviewValues [4]=mTicTacTextViews[4].getText().toString();
        mTextviewValues [5]=mTicTacTextViews[5].getText().toString();
        mTextviewValues [6]=mTicTacTextViews[6].getText().toString();
        mTextviewValues [7]=mTicTacTextViews[7].getText().toString();
        mTextviewValues [8]=mTicTacTextViews[8].getText().toString();

        mResult = (Button) findViewById(R.id.result_button);
        mResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                whoWon();
            }
        });
    }



    private void whoWon ()
    {
        mTextviewValues [0]=mTicTacTextViews[0].getText().toString();
        mTextviewValues [1]=mTicTacTextViews[1].getText().toString();
        mTextviewValues [2]=mTicTacTextViews[2].getText().toString();
        mTextviewValues [3]=mTicTacTextViews[3].getText().toString();
        mTextviewValues [4]=mTicTacTextViews[4].getText().toString();
        mTextviewValues [5]=mTicTacTextViews[5].getText().toString();
        mTextviewValues [6]=mTicTacTextViews[6].getText().toString();
        mTextviewValues [7]=mTicTacTextViews[7].getText().toString();
        mTextviewValues [8]=mTicTacTextViews[8].getText().toString();

        if(mTextviewValues[0].equals(mTextviewValues[1]) & mTextviewValues[1].equals(mTextviewValues[2]))
        {
            Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
        }
        else if (mTextviewValues[1].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[8]))
        {
            Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
        }
        else if (mTextviewValues[0].equals(mTextviewValues[3]) & mTextviewValues[3].equals(mTextviewValues[6]))
        {
            Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
        }
        else if (mTextviewValues[1].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[7]))
        {
            Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
        }
        else if (mTextviewValues[2].equals(mTextviewValues[5]) & mTextviewValues[5].equals(mTextviewValues[8]))
        {
            Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
        }
        else if (mTextviewValues[2].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[6]))
        {
            Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
        }

        else if (mTextviewValues[2].equals(mTextviewValues[4]) & mTextviewValues[4].equals(mTextviewValues[6]))
        {
            Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
        }
        else if (mTextviewValues[6].equals(mTextviewValues[7]) & mTextviewValues[7].equals(mTextviewValues[8]))
        {
            Toast.makeText(this,"Someone won",Toast.LENGTH_SHORT).show();
        }
        else Log.i("error" , "whowon didn't work");

    }
    @Override
    protected void  onSaveInstanceState(Bundle bundle)
    {
        super.onSaveInstanceState(bundle);
        bundle.putStringArray(TEXT_VIEW_VALUES_KEY,mTextviewValues);

    }
}
1

1 Answer 1

3

getText() doesn't take a parameter. mTicTacTextViews[i].getText() returns the text in the view. Are you trying to set the text in the view? If so, you want setText(CharSequence), not CharSequence getText().

Sign up to request clarification or add additional context in comments.

5 Comments

app crashing, with error "Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"
That implies that your TextView variable is null. Which makes sense, since you don't populate it until further down the (very, very long) method.
No, I do give mTextViewValues[] it text values in onCreate() not only in whoWon()
Yes, but the problem is that mTicTacTextViews[i] is null. Move your if(savedInstanceState != null) down near setOnClickListener and you should be better off.
Also, you'll find it a lot easer going if you break your onCreate down into multiple methods. Like createTextView(id), populateTextView(id), createXButton, createOButton and setupListener. Then you'll find it easier to spot where you're populating views before you create them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.