首页 > 解决方案 > 为什么 setText() 在恢复片段状态时不起作用

问题描述

我正在尝试完成一个井字游戏应用程序的编码,该应用程序使用相同片段的两个实例一起通信,以便每个玩家都可以在其自己的网格上播放,如此处所示(每个网格都放置在一个 xml 文件中,该文件替换了主框架中的框架布局xml)。该应用程序正常工作,但我无法在轮换后恢复应用程序的状态。为此,我使用ArrayList位于另一个名为的类中的静态ArrayPersistence

private static ArrayList<String> stringArrayList;

public ArrayPersistence(){}

public static void setStringArrayList(String[][] buttons)
{
    stringArrayList = new ArrayList<>(9);
    for(int i = 0; i < 3; i++)
        for(int y = 0; y < 3; y++)
        {
            stringArrayList.add(buttons[i][y]);
        }
    //System.out.println(stringArrayList);
}

public static ArrayList<String> getStringArrayList()
{
    //System.out.println(stringArrayList);
    return stringArrayList;
}

使用onSaveInstanceState我保存按钮上显示的文本的方法:

@Override
public void onSaveInstanceState(Bundle b)
{
    String[][] string = new String[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            string[i][j] = buttons[i][j].getText().toString();
        }
    }
    ArrayPersistence.setStringArrayList(string);

    int y = 1;
    b.putInt("rebooted", y);
    super.onSaveInstanceState(b);
}

随着onActivityCreated我尝试恢复文本

@Override
public void onActivityCreated(Bundle b)
{
    super.onActivityCreated(b);
    if(b != null)
    {
        if (b.getInt("rebooted") == 1)
        {
            ArrayList<String> string = ArrayPersistence.getStringArrayList();
            System.out.println("Arraylist printed by onActivityCreated: "+string);
            int i = 0;

            for (int x = 0; x < 3; x++)
                for (int y = 0; y < 3; y++)
                {
                    i++
                    String s = string.get(i);
                    buttons[x][y].setText(s);//<--- Problem here
                }
        }
    }
}

二维数组用于存储九个按钮的id。该应用程序在编译或运行时没有显示错误,我只是无法在按钮上设置保存的文本。我添加了一些 system.out 来打印整个ArrayList内容,以确保正确保存和检索所有内容,但不知道为什么setText()不能设置保存的文本


编辑#1

根据要求,这是用于获取按钮 id 的代码

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.layout_tris, container, false);

    this.ID = getArguments().getInt("id");

    buttons[0][0] = v.findViewById(R.id.b1_1);
    buttons[0][0].setOnClickListener(this);

    buttons[0][1] = v.findViewById(R.id.b1_2);
    buttons[0][1].setOnClickListener(this);

    buttons[0][2] = v.findViewById(R.id.b1_3);
    buttons[0][2].setOnClickListener(this);

    buttons[1][0] = v.findViewById(R.id.b2_1);
    buttons[1][0].setOnClickListener(this);

    buttons[1][1] = v.findViewById(R.id.b2_2);
    buttons[1][1].setOnClickListener(this);

    buttons[1][2] = v.findViewById(R.id.b2_3);
    buttons[1][2].setOnClickListener(this);

    buttons[2][0] = v.findViewById(R.id.b3_1);
    buttons[2][0].setOnClickListener(this);

    buttons[2][1] = v.findViewById(R.id.b3_2);
    buttons[2][1].setOnClickListener(this);

    buttons[2][2] = v.findViewById(R.id.b3_3);
    buttons[2][2].setOnClickListener(this);

    return v;
}

这是按钮 xml:

<Button
  android:id="@+id/b1_1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:textColor="#000000"
  android:textSize="30dp" />

3里面有9个按钮LinearLayouts

标签: javaandroidsettext

解决方案


好的,经过一些测试,我发现在任何方向更改后,托管片段的活动将重新启动并初始化 2 个新片段,因此setText将文本设置在旧片段而不是当前显示的片段上,这也解释了为什么android:freezesText="true"不起作用. 所以我修改onCreateMainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(savedInstanceState == null) {
        fragment1 = FragmentTris.newInstance(1)/*new FragmentTris(1)*/;
        fragment2 = FragmentTris.newInstance(2)/*new FragmentTris(2)*/;

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container1, fragment1, fragment1_tag)
                .replace(R.id.container2, fragment2, fragment2_tag)
                .commit();
    }
    else if(savedInstanceState != null)
    {
        fragment1 = (FragmentTris) getSupportFragmentManager().findFragmentByTag(fragment1_tag);
        fragment2 = (FragmentTris) getSupportFragmentManager().findFragmentByTag(fragment2_tag);
    }
    if(!fragment1.isInLayout() && !fragment2.isInLayout())
    {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container1, fragment1, fragment1_tag)
                .replace(R.id.container2, fragment2, fragment2_tag)
                .commit();
    }
}

这样在旋转后可以使用相同的片段实例并正确恢复按钮上的文本


推荐阅读