语音识别android中的异常

时间:2012-06-02 18:24:43

标签: android voice-recognition

我想识别音频中的声音。它将显示列表视图的已识别单词。我写了一个代码来执行工作,但得到一个致命的异常“android.content.ActivityNotFoundException”。

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

    tView=(TextView) findViewById(R.id.Textshow);
    vButton=(Button) findViewById(R.id.speakButton);
    lView=(ListView) findViewById(R.id.listView);
    vButton.setOnClickListener(this);
}

@Override
public void onClick(View arg0) 
{
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if(requestCode==REQUEST_CODE && resultCode==RESULT_OK)
    {
        ArrayList<String> result=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        lView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,result));
    }

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

这种例外的原因是什么?我怎么解决呢?感谢。

1 个答案:

答案 0 :(得分:0)

那是因为您的设备不支持该意图,请使用:

try {
startActivityForResult(intent, REQUEST_CODE);
} catch(ActivityNotFoundException e){
   Log.e("speech", e.toString());
}

并尝试在具有语音识别器的设备上进行测试,然后查看它是如何进行的。

另请看这里:ActivityNotFoundException + speech on S.O