2012-03-16 63 views
1

我把語音功能放到一個叫做voiceAssistants的新類中,但是當我啓動它時,它總是給我一個空指針異常錯誤。開始活動結果獲取空指針異常

語音助理:

public class VoiceAssistants { 
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; 
    private static VoiceActivityHelper voiceAssistants; 
    public VoiceAssistants(){ 
     voiceAssistants = new VoiceActivityHelper(); 
    } 
    /** 
    * Start voice input 
    */ 
    public void starVoiceAssistants(){ 
     this.voiceAssistants.startVoiceRecognitionActivity(); 
    } 

    private static class VoiceActivityHelper extends Activity { 

     private static ArrayList<String> matches; 

     public ArrayList<String> getResultSet(){ 
      return matches; 
     } 

     private void startVoiceRecognitionActivity() { 
      Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, String.format(
       "%s_%s", Locale.getDefault().getLanguage(), Locale 
         .getDefault().getCountry())); 
     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
       "Speech recognition demo"); 
         //error happen here!!! 
      startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 
     } 
    } 
} 

但是,當我把它改成,通過新項目產生的正常活動,也不會高的任何錯誤。 日誌信息:

E/AndroidRuntime(22769): java.lang.NullPointerException 
E/AndroidRuntime(22769): 
at android.app.Activity.startActivityForResult(Activity.java:2827) 
E/AndroidRuntime(22769):  
at panda.com.db.VoiceAssistants$VoiceActivityHelper.startVoiceRecognitionActivity(VoiceAssistants.java:56) 
E/AndroidRuntime(22769):  
at panda.com.db.VoiceAssistants$VoiceActivityHelper.access$1(VoiceAssistants.java:47) 
E/AndroidRuntime(22769):  
at panda.com.db.VoiceAssistants.starVoiceAssistants(VoiceAssistants.java:24) 

plz help,thank you !!

+0

你不能使用這樣的活動:voiceAssistants = new VoiceActivityHelper(); – 2012-03-16 14:57:31

+0

你可以發佈'startVoiceRecognitionActivity'的完整代碼嗎? – MByD 2012-03-16 14:58:41

+0

你能告訴我們什麼'RecognizerIntent.ACTION_RECOGNIZE_SPEECH'是什麼? 另外請確保您的Android清單中正確定義了該活動類? – wnafee 2012-03-16 15:01:04

回答

1

你打電話給startActivityForResult()但這不是Activity。你的Activity是一個私人的內部類,沒有其他人可以訪問。

+0

所以我需要將其更改爲公開? – panda 2012-03-16 15:45:06

+0

是的,你需要在你的清單中聲明它。 – CaseyB 2012-03-16 17:06:43