Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android,Java只从字符串数组中获取最后一个单词_Java_Android_Arrays_Contains - Fatal编程技术网

Android,Java只从字符串数组中获取最后一个单词

Android,Java只从字符串数组中获取最后一个单词,java,android,arrays,contains,Java,Android,Arrays,Contains,我构建了一个使用语音识别的java应用程序,我创建了如下字符串数组: String[] greetings = {"hello", "hi", "yow"}; 现在的问题是,应用程序只检测数组的最后一个字“yow”,而不是“hello”或“hi” 所以我真的不知道我做错了什么,也许mostLikelyThingHeard也需要一个“for”循环 完整代码: package nl.giovanniterlingen.pws; import java.util.ArrayList; impor

我构建了一个使用语音识别的java应用程序,我创建了如下字符串数组:

String[] greetings = {"hello", "hi", "yow"};
现在的问题是,应用程序只检测数组的最后一个字“yow”,而不是“hello”或“hi”

所以我真的不知道我做错了什么,也许
mostLikelyThingHeard
也需要一个“for”循环

完整代码:

package nl.giovanniterlingen.pws;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity implements OnInitListener {
    private static final String TAG = "PWS";

    private TextView result;

    private TextToSpeech tts;

    private Button speak;

    private int SPEECH_REQUEST_CODE = 1234;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        speak = (Button) findViewById(R.id.bt_speak);
        speak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRecognizeIntent();
            }
        });

        speak.setEnabled(false);
        result = (TextView) findViewById(R.id.tv_result);

        tts = new TextToSpeech(this, this);
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            speak.setEnabled(true);
        } else {
            // failed to init
            finish();
        }

    }

    private void sendRecognizeIntent() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Aan het luisteren...");
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 100);
        startActivityForResult(intent, SPEECH_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SPEECH_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                ArrayList<String> matches = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                if (matches.size() == 0) {
                    tts.speak("Ik heb niks gehoord, probeer het nog eens",
                            TextToSpeech.QUEUE_FLUSH, null);
                } else {
                    String mostLikelyThingHeard = matches.get(0);
                    result.setText("Dit heeft u gezegd: "
                            + mostLikelyThingHeard + ".");
                    String doei = "doei";
                    String[] greetings = { "hello", "hi", "yow" };

                    for (String strings : greetings) {

                        if (mostLikelyThingHeard.contains(strings)) {
                            tts.speak("Hey nice to see you!",
                                    TextToSpeech.QUEUE_FLUSH, null);

                        } else if (mostLikelyThingHeard.equals(doei)) {
                            tts.speak("Okay tot de volgende keer!",
                                    TextToSpeech.QUEUE_FLUSH, null);
                        } else {
                            tts.speak("Ik begrijp niet wat je bedoeld met "
                                    + mostLikelyThingHeard
                                    + " probeer het anders te verwoorden.",
                                    TextToSpeech.QUEUE_FLUSH, null);
                        }
                    }
                }
            } else {
                Log.d(TAG, "result NOT ok");
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        if (tts != null) {
            tts.shutdown();
        }
        super.onDestroy();
    }
}
包nl.giovanniterlingen.pws;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.speech.RecognizerIntent;
导入android.speech.tts.TextToSpeech;
导入android.speech.tts.TextToSpeech.OnInitListener;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
公共类主扩展活动实现OnInitListener{
私有静态最终字符串TAG=“PWS”;
私有文本视图结果;
私密文本语音tts;
私人按键通话;
专用int语音请求码=1234;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
speak=(按钮)findviewbyd(R.id.bt_speak);
speak.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
sendRecognizeIntent();
}
});
speak.setEnabled(false);
结果=(TextView)findViewById(R.id.tv_结果);
tts=新的TextToSpeech(这个,这个);
}
@凌驾
公共无效onInit(int状态){
if(status==TextToSpeech.SUCCESS){
speak.setEnabled(true);
}否则{
//初始化失败
完成();
}
}
私有void sendRecognitionIntent(){
意向意向=新意向(识别意向、行动、识别言语);
intent.putExtra(识别器intent.EXTRA_语言_模型,
识别者意图、语言、模型、自由形式);
intent.putExtra(RecognizerIntent.EXTRA_提示符,“Aan het luisteren…”);
intent.putExtra(识别器intent.EXTRA_最大结果,100);
startActivityForResult(意图、言语请求代码);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==语音请求码){
if(resultCode==RESULT\u OK){
ArrayList匹配=数据
.getStringArrayListExtra(识别器意图.额外结果);
如果(匹配.size()==0){
说话(“我不知道该怎么办,我不知道该怎么办”,
TextToSpeech.QUEUE_FLUSH,null);
}否则{
String mostLikelyThingHeard=matches.get(0);
result.setText(“Dit heeft u gezegd:”
+最有可能是“心+”);
字符串doei=“doei”;
String[]问候语={“你好”,“你好”,“你好”};
for(字符串:问候语){
if(mostLikelyThingHeard.contains(字符串)){
说:“嗨,很高兴见到你!”,
TextToSpeech.QUEUE_FLUSH,null);
}else如果(最可能的结果等于(doei)){
说话(“好的,我的孩子!”,
TextToSpeech.QUEUE_FLUSH,null);
}否则{
tts.speak(“Ik begrijp niet wat je bedoeld met”
+最有可能
+“可能是安德斯·特维尔伍德。”,
TextToSpeech.QUEUE_FLUSH,null);
}
}
}
}否则{
Log.d(标记“结果不正常”);
}
}
super.onActivityResult(请求代码、结果代码、数据);
}
@凌驾
受保护的空onDestroy(){
如果(tts!=null){
tts.shutdown();
}
super.ondestory();
}
}

我看到的唯一问题是,您正在检查for中的“doei”。。。除此之外,它应该可以正常工作。此外,考虑到听到的事情是在你预期的情况下。 此外,您还应该跟踪您在for中是否找到了一个单词

String doei = "doei";
String[] greetings = { "hallo", "hi", "yow" };
mostLikelyThingHeard = mostLikelyThingHeard.toLowerCase();
boolean found = false;

for (String strings : greetings) {
    if (mostLikelyThingHeard.contains(strings)) {
        tts.speak("Hey nice to see you!",
            TextToSpeech.QUEUE_FLUSH, null);
        found = true;
        break;
    }
} 
if (!found) { 
    if (mostLikelyThingHeard.equals(doei)) {
        tts.speak("Okay tot de volgende keer!", TextToSpeech.QUEUE_FLUSH, null);
    } else {
        tts.speak("Ik begrijp niet wat je bedoeld met "
          + mostLikelyThingHeard
          + " probeer het anders te verwoorden.",
          TextToSpeech.QUEUE_FLUSH, null);
    }
}

你可以把所有单词放在一个字符串中(不是数组字符串)。我知道这不是最好的解决方案,所以我给你提供了一个简单的例子,在这个例子中,无论你写什么,引擎都会说话

MainActivity.java
package com.authorwjf.talk2me;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    protected static final int REQUEST_OK = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);
    }


}
//The on click handler is responsible for firing off the voice intent. 

@Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
         i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
             try {
             startActivityForResult(i, REQUEST_OK);
         } catch (Exception e) {
                Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
         }
}

//When the intent calls back, we display the transcribed text.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_OK  && resultCode==RESULT_OK) {
                ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                ((TextView)findViewById(R.id.text1)).setText(thingsYouSaid.get(0));
        }
    }

//activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="132dp"
        android:text="..." ></TextView>

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:layout_marginTop="37dp"
        android:src="@android:drawable/ic_btn_speak_now" ></ImageButton>

</RelativeLayout>
MainActivity.java
包com.authorwjf.talk2me;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.speech.RecognizerIntent;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展活动实现OnClickListener{
受保护的静态最终int请求_OK=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findviewbyd(R.id.button1).setOnClickListener(this);
}
}
//点击处理程序负责触发语音意图。
@凌驾
公共void onClick(视图v){
意图i=新意图(识别者意图、行动、识别、讲话);
i、 putExtra(RecognizerIntent.EXTRA语言模型,“en-US”);
试一试{
startActivityForResult(i,请求_OK);
}捕获(例外e){
Toast.makeText(这是“将语音初始化为文本引擎时出错”),Toast.LENGTH_LONG.show();
}
}
//当intent回调时,我们显示转录的文本。
@凌驾
ActivityResult上受保护的void(int请求代码、int结果代码、,
MainActivity.java
package com.authorwjf.talk2me;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    protected static final int REQUEST_OK = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);
    }


}
//The on click handler is responsible for firing off the voice intent. 

@Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
         i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
             try {
             startActivityForResult(i, REQUEST_OK);
         } catch (Exception e) {
                Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
         }
}

//When the intent calls back, we display the transcribed text.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_OK  && resultCode==RESULT_OK) {
                ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                ((TextView)findViewById(R.id.text1)).setText(thingsYouSaid.get(0));
        }
    }

//activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="132dp"
        android:text="..." ></TextView>

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:layout_marginTop="37dp"
        android:src="@android:drawable/ic_btn_speak_now" ></ImageButton>

</RelativeLayout>