为什么即使在UI线程上也没有更改TextView的Text值

时间:2013-02-25 13:11:45

标签: android user-interface

我有一个带按钮的活动。在ACTION_DOWN上,我按下此按钮开始录制并继续录制。在ACTION_UP我必须停止录音。录制线程是独立的。活动上有一个文本视图,显示录制时间。 时间的默认值是“00:00:00:000” 从录制线程我调用活动类的updateTime方法:

public void updateTime(final String time) {
    Log.d("***UPDATE TIME Thread", "Current time:" + time);
    runOnUiThread(new Runnable() {
        public void run() {
            timerTextView.setText(time);
            Log.d("UPDATE TIME Thread", "Current time:"+time);
        }
    });
}

当我第一次调用此活动时,它可以正常工作并正确更新时间值。但是,如果我从前一个屏幕选择相同的选项以开始第二次记录,则时间文本视图的值保持与“00:00:00:000”相同,其中显示正确值的第二个Log.d代码行。这是LogCat

02-25 16:07:10.660: D/Recording(5075):  Start
02-25 16:07:10.695: D/***UPDATE TIME Thread(5075): Current time:00:00:00:032
02-25 16:07:10.711: D/UPDATE TIME Thread(5075): Current time:00:00:00:032
02-25 16:07:10.843: D/***UPDATE TIME Thread(5075): Current time:00:00:00:174
02-25 16:07:10.863: D/UPDATE TIME Thread(5075): Current time:00:00:00:174
02-25 16:07:10.945: D/***UPDATE TIME Thread(5075): Current time:00:00:00:280
02-25 16:07:10.972: D/UPDATE TIME Thread(5075): Current time:00:00:00:280
02-25 16:07:11.082: D/***UPDATE TIME Thread(5075): Current time:00:00:00:418
02-25 16:07:11.089: D/UPDATE TIME Thread(5075): Current time:00:00:00:418
02-25 16:07:11.203: D/***UPDATE TIME Thread(5075): Current time:00:00:00:535
02-25 16:07:11.222: D/UPDATE TIME Thread(5075): Current time:00:00:00:535
02-25 16:07:11.351: D/***UPDATE TIME Thread(5075): Current time:00:00:00:676
......
Here is my Activity Class Code

public class VerficationPBActivity extends Activity实现了VoiceManagerListener,OnTouchListener {     私人索赔人currentClaimant = null;     TextView screenHeading = null;     private String screenHeadingText = null;     TextView textToSpeakTextView = null;     TextView timerTextView = null;     TextView statusHintTextView = null;     私人按钮pushToRecordButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verfication_pb);
    ActivityController.getActivityController().setCurrentActivity(this);
    screenHeading = (TextView) findViewById(R.id.screenHeading);
    textToSpeakTextView = (TextView) findViewById(R.id.text_to_speek);
    timerTextView = (TextView) findViewById(R.id.timer);
    statusHintTextView = (TextView) findViewById(R.id.status_hint);
    screenHeadingText = (String) getIntent().getSerializableExtra("heading");
    currentClaimant = (Claimant) getIntent().getSerializableExtra("claimant");
    screenHeading.setText(screenHeadingText);
    if (currentClaimant != null) {
        textToSpeakTextView.setText(currentClaimant.getNextPrompt());
        statusHintTextView.setText(currentClaimant.getStatusHint());
    }
    pushToRecordButton = (Button) findViewById(R.id.pushToRecord);
    pushToRecordButton.setOnTouchListener(this);

}

@Override
public void updateTime(final String time) {

    Log.d("***UPDATE TIME Thread", "Current time:" + time);
    runOnUiThread(new Runnable() {
     public void run() {
     timerTextView.setText(time);
     Log.d("UPDATE TIME Thread", "Current time:"+time);
     Log.d("UPDATE TIME Thread", "Current value:"+timerTextView.getText());
     }
     });

}




@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        VoiceManager.getInstance().startRecording();
        break;
    case MotionEvent.ACTION_UP:
        Log.d("Push Button Down", "Up Event Fired");
        VoiceManager.getInstance().stop();
        String audioDataBase64 = VoiceManager.getInstance().getAudioDateinBase64Encoding();
        if (screenHeadingText.equals("Verification")) {
            VBSServiceManager.getInstance().processVerification(currentClaimant, audioDataBase64);
        } else {
            VBSServiceManager.getInstance().processEnrollment(currentClaimant, audioDataBase64);
        }
        break;
    }
    return false;
}

}

1 个答案:

答案 0 :(得分:0)

在android中,您无法从其他线程更新UI。这是android开发中的限制(我认为这是消除不必要的错误的一个特性)。你可以使用AsyncTask ......

在使用中,您可以在donInBackground()中执行长任务,并且可以使用onProgressUpdate()更新UI ...请参阅AsyncTask示例here

修改

请参阅this类似问题以获取更多信息......