2011-10-31 67 views
1

我有这样的代码:活动和语音识别

public class VoiceActivity extends Activity implements OnClickListener { 

private TextView mText; 
private SpeechRecognizer sr; 
private static final String TAG = "MyActivity"; 
public String str; 

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

    Button speakButton = (Button) findViewById(R.id.speakButton); 
    mText = (TextView) findViewById(R.id.textView1); 

    speakButton.setOnClickListener(this); 
    sr = SpeechRecognizer.createSpeechRecognizer(this); 
    sr.setRecognitionListener(new listener()); 
} 

class listener implements RecognitionListener { 
    public void onReadyForSpeech(Bundle params) { 
     Log.d(TAG, "onReadyForSpeech"); 
    } 

    public void onBeginningOfSpeech() { 
     Log.d(TAG, "onBeginningOfSpeech"); 
    } 

    public void onRmsChanged(float rmsdB) { 
     Log.d(TAG, "onRmsChanged"); 
    } 

    public void onBufferReceived(byte[] buffer) { 
     Log.d(TAG, "onBufferReceived"); 
    } 

    public void onEndOfSpeech() { 
     Log.d(TAG, "onEndofSpeech"); 
    } 

    public void onError(int error) { 
     Log.d(TAG, "error " + error); 
     mText.setText("error " + error); 
    } 

    public void onResults(Bundle results) { 
     str = new String(); 
     Log.d(TAG, "onResults " + results); 
     ArrayList<String> data = results 
       .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     for (int i = 0; i < data.size(); i++) { 
      Log.d(TAG, "result " + data.get(i)); 
      str += data.get(i); 
     } 

     mText.setText("results: " + str); 

    } 

    public void onPartialResults(Bundle partialResults) { 
     Log.d(TAG, "onPartialResults"); 
    } 

    public void onEvent(int eventType, Bundle params) { 
     Log.d(TAG, "onEvent " + eventType); 
    } 
} 

public void onClick(View v) { 
    if (v.getId() == R.id.speakButton) { 
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.moc"); 
     intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); 
     sr.startListening(intent); 
    } 
} 

}

如何做出的语音识别后,另一活动的自动转换(按按钮 - 说 - 开放的下一个活动,并导致其) ?在我的例子,它说,在线路故障

intent.setClass (this, SecondActivity.class).

例子:

public void onResults(Bundle results) { 
    str = new String(); 
    Log.d(TAG, "onResults " + results); 
    ArrayList<String> data = results 
      .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
    for (int i = 0; i < data.size(); i++) { 
     Log.d(TAG, "result " + data.get(i)); 
     str += data.get(i); 
    } 
    Intent intent = new Intent(); 
    Bundle b = new Bundle(); 
     b.putString("StrID", str); 
     intent.putExtras(b); 
    intent.setClass(this, SecondActivity.class); 
    startActivity(intent); 
} 
+0

什么是错误?添加异常的堆栈跟踪,并验证您是否在清单文件上声明了您的SecondActivity – Houcine

+0

**错误:** Intent类型的方法setClass(Context,Class )不适用于参数(VoiceActivity.listener ,Class ) – monomi

+0

=请参阅我的回答:) – Houcine

回答

0

你应该relpace的thisVoiceActivity.this,因为this是它交给了listener实例不是你的活动的背景下:

intent.setClass (VoiceActivity.this, SecondActivity.class); 
+0

真的!我很愚蠢:(非常感谢你。 – monomi

+0

欢迎你,我们在这里帮助:) – Houcine