无法将getString用于textView - android

时间:2018-06-04 05:49:11

标签: java android

我遇到TextView中字符串的OnRestoreInstanceState问题。该字符串基本上是一个箭头(<<<<<<<<<<<<<<<

基本上这是我的代码:

private TextView textViewPlayerOneTurn;

...

    private void switchPlayerTurn(){
        if (playerOneMove){
            textViewPlayerOneTurn.setVisibility(View.VISIBLE);
            textViewPlayerTwoTurn.setVisibility(View.INVISIBLE);
        }
        else{
            textViewPlayerOneTurn.setVisibility(View.INVISIBLE);
            textViewPlayerTwoTurn.setVisibility(View.VISIBLE);
        }

...

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putString("textViewPlayerOneTurn", textViewPlayerOneTurn.toString());
        outState.putString("textViewPlayerTwoTurn", textViewPlayerTwoTurn.toString());
        outState.putBoolean("playerOneMove", playerOneMove);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        playerOneMove = savedInstanceState.getBoolean("playerOneMove");
    }

我的问题是,当我旋转屏幕时,指针始终默认为玩家转弯。我可能正在咆哮错误的树但我的想法是onRestoreInstanceState所以我想在该方法中包含以下内容:

textViewPlayerOneTurn = savedInstanceState.getString(" textViewPlayerOneTurn");     textViewPlayerTwoTurn = savedInstanceState.getString(" textViewPlayerTwoTurn");

但它给出了错误,说它需要 android.widget.TextView ,并且发现 Java.lang.String

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

textViewPlayerOneTurn.toString()返回TextView对象的字符串表示形式,该对象在TextView类的toString()方法中定义。

textViewPlayerOneTurn.getText().toString()将返回TextView上显示的预期字符串。

而不是textViewPlayerOneTurn.toString()使用textViewPlayerOneTurn.getText().toString()

答案 1 :(得分:0)

请使用

textViewPlayerOneTurn.getText().toString()

而不是

textViewPlayerOneTurn.toString().