Android屏幕旋转后TableRow文本视图内容错误

Android屏幕旋转后TableRow文本视图内容错误,android,textview,android-tablelayout,Android,Textview,Android Tablelayout,我在片段中有一个TableLayout,一旦片段通过以下简化测试代码创建,我就会生成行: public void displayChar(String ClassID) { Character charac = new Character(Integer.parseInt(ClassID), getActivity()); TableLayout stanceListTable = (TableLayout) getView().findViewById(R.id.charSta

我在片段中有一个TableLayout,一旦片段通过以下简化测试代码创建,我就会生成行:

public void displayChar(String ClassID) {
    Character charac = new Character(Integer.parseInt(ClassID), getActivity());
    TableLayout stanceListTable = (TableLayout) getView().findViewById(R.id.charStanceList);
    stanceListTable.removeAllViews();
    i=0;
    for (Character.StanceCondition stanceCondition : stanceList) {
        TableRow row=createTableRow(stanceCondition);
        ((TextView)row.findViewById(R.id.stances)).setText(Integer.toString(i));
        Log.i("RowContent",((TextView)row.findViewById(R.id.stances)).getText().toString());
        stanceListTable.addView(row);
        i++;
    }
}
在第一次创建仅包含此片段的“我的活动”时 我有很好的输出: 但在轮换之后:

但最糟糕的是日志是正确的

以前

08-02 12:30:37.446    5697-5697/com.lectem.gecharacters I/RowContent﹕ 0
08-02 12:30:37.449    5697-5697/com.lectem.gecharacters I/RowContent﹕ 1
08-02 12:30:37.466    5697-5697/com.lectem.gecharacters I/RowContent﹕ 2
08-02 12:30:37.469    5697-5697/com.lectem.gecharacters I/RowContent﹕ 3
之后

createTableRow方法:

public TableRow createTableRow(Character.StanceCondition stanceCondition) {
    TableRow row =(TableRow) ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.stance_list_row,null);
    TextView hands=(TextView)row.findViewById(R.id.stanceHands);
    hands.setText(stanceCondition.RHand + stanceCondition.LHand);

    TextView stancesView =(TextView)row.findViewById(R.id.stances);

    MovementMethod movementMethod = stancesView.getMovementMethod();
    if ((movementMethod == null) || !(movementMethod instanceof LinkMovementMethod))
    {
        stancesView.setMovementMethod(LinkMovementMethod.getInstance());
    }
    Log.i("row","creating row");
    SpannableString finalss=new SpannableString("");
    /*Should be creating my spannable string here with links, but I simplified the code and changed the text after creation of the row*/
    stancesView.setText(finalss);
    return row;
}
为什么它会显示错误的文本,而getText方法返回好的内容。。。第一个文本视图有好的文本。 这真的让我发疯了,有什么问题吗

编辑: 这是我的活动的onCreate功能:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_char_details);
    ClassID = getIntent().getExtras().getString("ClassID");
    mDetailsFragment = (CharDetailsFragment) getSupportFragmentManager().findFragmentById(
            R.id.charDetails);
    mDetailsFragment.displayChar(ClassID);

}
对于我的片段:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_char_details, container, false);

}
编辑2:

问题似乎来自于

    MovementMethod movementMethod = stancesView.getMovementMethod();
    if ((movementMethod == null) || !(movementMethod instanceof LinkMovementMethod))
    {
        stancesView.setMovementMethod(LinkMovementMethod.getInstance());
    }

有什么办法可以解决吗?

在xml文本视图上尝试android:freezesText=true,这可能会解决你的问题。

所以我找到了解决我问题的方法,不过如果有人能准确解释它的工作原理,我会接受他的答案


我只是通过片段的onStart方法调用displayChar方法。似乎当调用onClose时,LinkMovementMethod将出现bug。因此,使用OnStart重新生成TextView似乎是可行的。

问题是,每行中的所有TextView都具有相同的id。因此,当保存每行的已保存实例状态时,它会覆盖上一行的已保存状态,并在最后保存最后一行的状态。然后,当状态恢复时,它会用id R.id.stances和最后一行的值填充所有视图。

我认为您所描述的问题不会发生在您发布的代码中,因为我没有看到任何可能导致这一错误之王的错误。你如何处理旋转?您是否使用某些东西保存到不同对象的状态?在这段代码和将此视图返回给用户的那一刻之间,您是否调用了什么?我添加了onCreate方法,但我怀疑它是否有帮助。movementMethod似乎是问题的根源。当您以横向模式启动应用程序并旋转到纵向模式时,会出现什么行为?与你通常做的相反。当你旋转它,然后旋转回来,然后再旋转时,它会做什么?在任何模式下启动应用程序时,它都可以正常工作,并且只有在旋转后才有这种行为…那么为什么它在没有LinkMovementMethod的情况下工作?MovementMethod不会在保存的实例状态下添加。但当状态恢复时,在TextView上调用setText,这将设置必要的LinkMovementMethod。我还是不明白,因为当我通过getText检查文本时,它是好的。只是显示错误。如前所述,您需要通过onStart方法重新生成视图我不知道在onStart上重新生成视图是什么意思。
    MovementMethod movementMethod = stancesView.getMovementMethod();
    if ((movementMethod == null) || !(movementMethod instanceof LinkMovementMethod))
    {
        stancesView.setMovementMethod(LinkMovementMethod.getInstance());
    }