我的代码出了什么问题?我不知道该怎么办

时间:2016-03-30 00:26:52

标签: string text record speech

这是我的源代码,它在Ide中没有显示错误,但是它停在设备上。

我用语言对程序说些什么,而程序取决于记录,给我看不同的答案。

我不知道还有什么。问题显然是从methodod setText开始的。

谢谢你

 protected static final int RESULT_SPEACH =1;
 private Button record, speak;

 TextToSpeech t1;
 String SpokedText;
 private EditText txtView;
 private EditText txtView2;
 TextToSpeech t1;



 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_mai);
 record = (Button) findViewById(R.id.record);
 speak = (Button) findViewById(R.id.speak);

speak.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        record();

    }
});
 }
public void record() {
    Intent reconize = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    reconize.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    reconize.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    reconize.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.Say_something));

    try {
        startActivityForResult(reconize, RESULT_SPEACH);

    } catch (ActivityNotFoundException a) {
        Toast t = Toast.makeText(getApplicationContext(), "Tu dispositivo no puede ejecutar este tipo de operaciones", Toast.LENGTH_LONG);
        t.show();
 }
 }

 @Override
 public void onActivityResult (int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode,resultCode,Data);
switch (requestCode) {
    case RESULT_SPEACH: {
        if (resultCode == RESULT_OK && null != Data) {
        SpokedText = data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);
                txtView2.setText(SpokedText);
                setText(SpokedText);

                }break;
            }
        }
    }
  public void setText(String string){
    if (txtView2.equals("Good Morning")){
        txtView.setText("Good Morning Sir");
    }else if(txtView2.equals("I need your help")){
        txtView.setText("Ok, ¿How can i help you?");
    }

            }  
   @Override
   public void onInit(int status) {
   if (status== TextToSpeech.SUCCESS){
        int result = t1.setLanguage(Locale.getDefault());
        if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
            Log.e("TTS", "This Lenguaje is not supported");
        }else{
            speakOut();
        }
    }

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void speakOut(){
    String text = txtView.getText().toString();


    t1.speak(text, TextToSpeech.QUEUE_FLUSH, null);

}
}

问题可能出现在AndroidManifest Xml中吗?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.dower.myapplication">

    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


    <activity android:name=".PantallaDeInicio"
        android:label="@string/StarScreen"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.PICK_ACTIVITY"></action>
            <category android:name="android.intent.category.LAUNCHER"></category>
        </intent-filter>

    </activity>

    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".Casanova"
                    android:label="@string/casaNova"
                    android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.PICK_ACTIVITY"></action>
                <category android:name="android.intent.category.APP_BROWSER"></category>
                <action android:name="android.intent.action.VOICE_COMMAND"></action>
                <category android:name="android.intent.category.VOICE"></category>
                

            </intent-filter>
        </activity>

        <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

问题是你无法将控件与字符串等同起来。

  if (txtView2.equals("Good Morning")){

应该是

if (txtView2.getText().toString().equals("Good Morning")){

每次要检查TextView控件中的字符串时,都需要执行相同的操作