【问题标题】:Why won't my TextView update/reset in android?为什么我的 TextView 不会在 android 中更新/重置?
【发布时间】:2013-06-25 18:54:46
【问题描述】:

我创建了一个 TextView,public TextView textView;,稍后我将在 onCreate 的 MainActivity.java 中定义它:

    textView = (TextView) findViewById(R.id.textViewName);

但是,当我设置文本时,它没有正确更新。每当我使用setText 方法时,它都不会在屏幕上更新。对setText 的初始调用是在一个名为recordClap() 的方法中。

    /**set text view*/
    textView.setText("listening...");

此文本不会更新到屏幕上。

最后,我将文本设置为显示“成功!”一旦满足某些条件。

    textView.setText("Success!");

由于某种原因,这是对“setText”的唯一调用。

那么,为什么 TextView 没有正确更新新文本?有什么我遗漏的吗?

完整代码如下:

public class MainActivity extends Activity{

private static final String TAG = "Clapper";
private static final long DEFAULT_CLIP_TIME = 1000;
private long clipTime = DEFAULT_CLIP_TIME;

/**create text view*/
public TextView textView;
private boolean continueRecording;
public static final int AMPLITUDE_DIFF_LOW = 10000;
public static final int AMPLITUDE_DIFF_MED = 18000;
public static final int AMPLITUDE_DIFF_HIGH = 32767; 
private int amplitudeThreshold=AMPLITUDE_DIFF_HIGH;
private MediaRecorder recorder = null;
private static String tmpAudioFile = null;

public boolean recordClap()
{
/**set text view*/
textView.setText("listening...");

Log.i(TAG, "record clap");
boolean clapDetected = false;

try
{

    tmpAudioFile = Environment.getExternalStorageDirectory().getAbsolutePath();
    tmpAudioFile += "/audiorecordtest.3gp";

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(tmpAudioFile);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.prepare();

    Log.i(TAG, "i've been prepared!");
}
catch (IOException io)
{
    Log.e(TAG, "failed to prepare recorder ", io);
}

recorder.start();
int startAmplitude = recorder.getMaxAmplitude();
Log.i(TAG, "starting amplitude: " + startAmplitude);

do
{
    Log.i(TAG, "waiting while recording...");
    waitSome();
    int finishAmplitude = recorder.getMaxAmplitude();
    int ampDifference = finishAmplitude - startAmplitude;
    if (ampDifference >= amplitudeThreshold)
    {
        Log.w(TAG, "heard a clap!");
        /**here is the output to screen*/
        /**reset text view*/
        textView.setText("Success!");

        clapDetected = true;
    }
    Log.d(TAG, "finishing amplitude: " + finishAmplitude + " diff: "
            + ampDifference);
} while (continueRecording || !clapDetected);

Log.i(TAG, "stopped recording");
done();

return clapDetected;
}

private void waitSome()
{
try
{
    // wait a while
    Thread.sleep(clipTime);
} catch (InterruptedException e)
{
    Log.i(TAG, "interrupted");
}
}

public void done()
{
Log.d(TAG, "stop recording");
if (recorder != null)
{
    if (isRecording())
    {
        stopRecording();
    }
        //now stop the media player
        recorder.stop();
        recorder.release();
    }
}

public boolean isRecording()
{
    return continueRecording;
}

public void stopRecording()
{
    continueRecording = false;
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("hello", "world!");
    setContentView(R.layout.activity_main);

    /**define text view*/
    textView = (TextView) findViewById(R.id.textViewName);

    /**run*/
    recordClap();

/**Restart Button*/    
final Button button = (Button) findViewById(R.id.button_id);
    button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
        recordClap();
    }
  });
}
} 

【问题讨论】:

  • 初始文本为空 - 未分配任何内容
  • 在您点击按钮之前 - 文本是否为空?
  • 我的猜测是它发生得如此之快以至于你看不到它。您是否尝试过缓慢地步进(调试)程序以确保其发生变化?通过查看您的代码,我几乎可以肯定 setTexts 正在被调用。
  • 我已经设置了 Log.i() 消息以确保它们通过这些步骤,但我没有尝试过步进 - 我现在会这样做 - 感谢您的建议
  • 我检查了你的代码。正如@Jack 所说,“等待...”过得如此之快,因为在那之后您正在调用recordClap 方法。它在我的设备上更改为listening...。我没有检查success 部分,因为你说它正在工作,而且我没有任何声音文件我删除了你的这部分代码。正如他所说,尝试逐步调试。

标签: java android text textview settext


【解决方案1】:

正如我在我的 cmets 中所说,变化发生得如此之快,以至于你看不到它。尝试放慢速度,或者增加你的 waitSome 可能吗?但请注意 - 如果我在您的 waitSome() 方法中没记错的话,您是在 UI 线程上的 Thread.sleeping - 这将导致 UI 在此期间不更新。所以它可能会像这样发生(我不记得 UI 线程队列是如何工作的):

  1. recordClap 被调用
  2. textView.setText("listening") 被调用并放入队列(但 UI 还没更新)
  3. Thread.sleep(1000) 暂停 UI 线程
  4. TextView 说“正在听”片刻然后继续

【讨论】:

  • +1 - 我认为文本没有正确更改是因为 UI 的更新方式 - 而不是 .setText 我使用了 .append 并且我想要的所有文本都出现了,但是之后整个过程已经结束。所以本质上,由于 UI 线程的工作方式,我正在重写 .setText,然后最新版本(读取“成功!”的版本)正在打印到屏幕上
【解决方案2】:

请参考 textview 即 textView = (TextView) findViewById(R.id.textViewName); 在设置文本之前在 recordclap() 函数中。

谢谢

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多