【发布时间】: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