我制作了一个使用语音识别的安卓应用程序。问题是它显示空指针异常。
显示错误的代码是:
public void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Give Me An Order!");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
/**
* Handle the results from the recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
//store the result that the user might have said but google thinks he has said that only
ArrayList<String> r=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String[] result = new String[r.size()];
result = r.toArray(result);
Intent i=new Intent(getApplicationContext(),search.class);
if(result[0].length()<=0)
{
showToast("Could not fetch command properly try again");
}
else
{
i.putExtra("q", result[0]);
startActivity(i);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
在线上发生错误 if(result[0].length()<=0)