错误更新Android应用中的TextView

时间:2013-07-17 14:00:51

标签: java android textview android-studio

我使用最新的Android Studio创建简单的Android应用(https://www.linux.com/learn/docs/683628-android-programming-for-beginners-part-1)。代码:

public class test_act extends Activity {

    private static final int MILLIS_PER_SECOND = 1000;
    private static final int SECONDS_TO_COUNTDOWN = 30;
    private android.widget.TextView     countdownDisplay;
    private android.os.CountDownTimer timer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_act);

        countdownDisplay = (android.widget.TextView) findViewById(R.id.time_display_box);
        android.widget.Button startButton = (android.widget.Button) findViewById(R.id.startbutton);
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                try {
                    showTimer(SECONDS_TO_COUNTDOWN * MILLIS_PER_SECOND);
                } catch (NumberFormatException e) {
                    // method ignores invalid (non-integer) input and waits
                    // for something it can use
                }
            }
        });
    }
    private void showTimer(int countdownMillis) {
        if(timer != null) { timer.cancel(); }
        timer = new android.os.CountDownTimer(countdownMillis, MILLIS_PER_SECOND) {
            @Override
            public void onTick(long millisUntilFinished) {
                countdownDisplay.setText("counting down: " +
                        millisUntilFinished / MILLIS_PER_SECOND);
            }
            @Override
            public void onFinish() {
                countdownDisplay.setText("KABOOM!");
            }
        }.start();
    }
}

我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
    <TextView
            android:id="@+id/time_display_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="60dp"
            android:text="@string/_00_30"
            android:textAppearance="?android:attr/textAppearanceLarge"/>
    <Button
            android:id="@+id/startbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/time_display_box"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="41dp"
            android:text="@string/start" />

</RelativeLayout>

在模拟器中它很好用。但在我的Galaxy S2与CyanogenMod10.1(Android 4.2.2)应用程序错误更新TextView。截图: enter image description here

我如何解决这个问题?

upd:屏幕旋转后TextView更新一次。

2 个答案:

答案 0 :(得分:0)

您可能希望每次更新时都尝试使布局无效。我猜测文本的更新频率,手机没有足够的时间重新绘制布局。这也可以解释为什么它在您旋转手机时起作用,因为这样就迫使布局更新。

countdownDisplay.invalidate();

如果这不起作用,请告诉我。

答案 1 :(得分:0)

当您将UI更新放在try块中时,通常会发生这种情况,尝试避免它或使用runOnUiThread包装。
编辑:

另一个原因 - 您将其更新为快速 - 您的代码每秒执行1000次更新,我认为它无法处理它。

相关问题