我正在使用片段布局,当我说完停止工作时。 我需要补充一点。
package com.example.textspeech;
import java.util.ArrayList;
import java.util.Locale;
import javax.xml.transform.Result;
import android.app.Activity;
import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import static android.app.Activity.RESULT_OK;
public class Voice extends Fragment {
//private static final int RESULT_OK = Activity.RESULT_OK;
//private static final int RESULT_OK = 0;
private TextView txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
public Voice(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.voice_to_text, container, false);
txtSpeechInput = (TextView) getActivity().findViewById(R.id.txtSpeechInput);
// hide the action bar
//getActivity().getActionBar().hide();
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
btnSpeak = (ImageButton) getActivity().findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//promptSpeechInput();
Toast.makeText(getActivity(), "Say Something", 10000).show();
promptSpeechInput();
}
private void promptSpeechInput() {
// TODO Auto-generated method stub
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getActivity(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT : {
if (resultCode ==RESULT_OK && null != data) {
Toast.makeText(getActivity(), "ok", 12345).show();
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
}
当我按下按钮时,谷歌提示打开成功,但当我说完后,应用程序停止响应。 请帮忙
答案 0 :(得分:0)
将此行添加到导入:
import static android.app.Activity.RESULT_OK;
RESULT_OK
中没有Fragment
字段,因此没有导入就无法工作;当您尝试Activity.RESULT_OK
时,您可能忘记导入Activity
。