这是我的代码:
Voice.onSpeechStart = this.onSpeechStart.bind(this);
Voice.onSpeechRecognized = this.onSpeechRecognized.bind(this);
Voice.onSpeechResults = this.onSpeechResults.bind(this);}
componentWillUnmount() {
Voice.destroy().then(Voice.removeAllListeners);}
onSpeechStart(e) {
this.setState({
started: "√"}
);}
onSpeechRecognized(e) {
this.setState({
recognized: "√"
});
}
onSpeechResults(e) {
this.setState({results: e.value});
if(matchSorter(this.setState.results,'login now') === 'login now'){
this.props.navigation.navigate("Dashboard");
}
else {
ToastAndroid.show("Please Try Again", ToastAndroid.SHORT);
}
}
async _startRecognition(e) {
this.setState({
recognized: "",
started: "",
results: []
});
try {
await Voice.start("en-US");
} catch (e) {
console.error(e);
}
render() {
return (
<Text>Say "Login now"</Text>
<TouchableOpacity
onPress={this._startRecognition.bind(this)}>
<Text> TAP HERE </Text>}
出现此错误:
基于Android的移动设备的错误图片:
*当我说现在登录时,“ this.setState.results ”的输出为“ 现在登录博客现在登录现在登录现在正在跑步现在登录”。
答案 0 :(得分:0)
当您尝试从状态中检索值时,您必须编写如下代码: this.state.VARIBLENAME 。您输入的this.setState.VARIBLENAME错误。
从您的代码中更正:
if(matchSorter(this.state.results,'login now') === 'login now'){
this.props.navigation.navigate("Dashboard");
}
您的整个代码:
Voice.onSpeechStart = this.onSpeechStart.bind(this);
Voice.onSpeechRecognized = this.onSpeechRecognized.bind(this);
Voice.onSpeechResults = this.onSpeechResults.bind(this);}
componentWillUnmount() {
Voice.destroy().then(Voice.removeAllListeners);}
onSpeechStart(e) {
this.setState({
started: "√"}
);}
onSpeechRecognized(e) {
this.setState({
recognized: "√"
});
}
onSpeechResults(e) {
this.setState({results: e.value});
if(matchSorter(this.state.results,'login now') === 'login now'){
this.props.navigation.navigate("Dashboard");
}
else {
ToastAndroid.show("Please Try Again", ToastAndroid.SHORT);
}
}
async _startRecognition(e) {
this.setState({
recognized: "",
started: "",
results: []
});
try {
await Voice.start("en-US");
} catch (e) {
console.error(e);
}
render() {
return (
<Text>Say "Login now"</Text>
<TouchableOpacity
onPress={this._startRecognition.bind(this)}>
<Text> TAP HERE </Text>
}
您可以替换代码,它会为您工作。