我目前正面临一个小挑战,我无法在改变它的方法(startRecording())之外获取对全局变量(fileName)所做的更改。
现在发生了什么:在startRecording()
更改后,fileName保持为null我想要的是:获取userRecordButton内的fileName onClick
这看起来像是一个基本问题,但我无法解决它,对获得快速帮助会非常有帮助。先感谢您。
p / s:这只是我认为相关的部分代码,如果您需要完整代码,请通知我。
代码
public class WordAdapter extends ArrayAdapter<Word> {
public String mFileName = null;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Button userRecordButton = (Button)listItemView.findViewById(R.id.recordButton);
userRecordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isRecording) {
userRecordButton.setText("Ready?");
Handler delayRecord = new Handler();
//This is where i called the handler
setDelayedRecord(delayRecord, userPlayButton, userRecordButton);
//The issue is here: it shows null when i tried debugging
String whatIsMyFileName = mFileName;
Handler maximumRecord = new Handler();
setMaximumRecord(maximumRecord, userPlayButton, userRecordButton);
} else {
exeStopRecording(userPlayButton, userRecordButton);
}
}
}
});
} //End of getView
public void setDelayedRecord(Handler handler, final Button userPlayBtnReference, final Button userRecordBtnReference) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
//James!, this is where I called start recorder.
startRecording();
userPlayBtnReference.setEnabled(false);
} catch (IllegalStateException e) {
Log.d("Hello", "Problem in start recording", e);
}
userRecordBtnReference.setText("Stop");
Toast.makeText(mContext, "Started Recording", Toast.LENGTH_SHORT).show();
setPathName(mFileName);
}
}, 500);
}
private void startRecording() {
mFile = new File(Environment.getExternalStorageDirectory(), mFolder_main);
if (!mFile.exists()) {
mFile.mkdirs();
}
formatter = new SimpleDateFormat("yyMMdd'-'HHmmssSSS");
currentTime_1 = new Date();
dateString = formatter.format(currentTime_1);
uniqueName = "Pronunce-" + dateString + ".3gp";
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/" + mFolder_main + "/" + uniqueName;
//mFileName should no longer be null
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
isRecording = true;
mRecorder.start();
}
}//End of wordAdapter
答案 0 :(得分:1)
您有一个全局变量mFileName,它已设置为null。然后,您将进入事件处理程序并将新的局部变量whatIsMyFileName设置为全局变量的值null。事件处理程序中的任何时候都不会调用任何会改变局部变量或全局变量值的方法。