2013-07-17 26 views
0

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

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> 

在模拟器这是很好的工作。但在我的银河S2与CyanogenMod10.1(Android 4.2.2)应用程序错误更新TextView。屏幕截图: enter image description here

我该如何解决这个问题?

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

+0

你可以添加一张图片,看看它应该是什么样子并发布你的xml文件? – buczek

+0

buczek,在模拟器中:http://s017.radikal.ru/i415/1307/3a/cfddc768498a.png – pkot

+0

我的XML我添加到问题。 – pkot

回答

0

您可能想在每次更新时尝试使布局无效。我在猜测文本更新的频率,手机没有足够的时间来重新绘制布局。这也可以解释为什么当你旋转你的手机时,因为布局被强制更新。

countdownDisplay.invalidate(); 

让我知道这是行不通的。

+0

它不工作=(我在countdownDisplay.setText(“倒计时:”+ millisUntilFinished/MILLIS_PER_SECOND);“和”countdownDisplay.setText(“KABOOM!”);“之前添加了此计数。”。 – pkot

+0

您是否尝试过其他设备? – buczek

+0

不,我没有其他设备,但我可以说我的朋友尝试...它需要10-20分钟... – pkot

0

它通常发生在将UI更新放入try块中时,尝试避免它或用runOnUiThread包装。
编辑:

另一个原因 - 你更新快 - 你的代码确实每秒我不认为它可以处理1000次更新。