startVoiceRecognitionActivity上的空指针

时间:2012-08-18 17:03:44

标签: android nullpointerexception

我一直在研究以下代码:

package com.lorenjz.phoneremotefive;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.widget.Toast;


public class BlueInterface extends Service implements TextToSpeech.OnInitListener{
    static VoiceStuff carpart;
    Context context;
    private TextToSpeech tts;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub

        return null;
    }
    @Override
    public void onCreate(){
        super.onCreate();
        Log.d("interface","interface onStart called");
        carpart = new VoiceStuff(context);
        tts = new TextToSpeech(this, this);

        PackageManager pm = getPackageManager();
        List <ResolveInfo> activities = pm.queryIntentActivities(new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);
        if (activities.size()!=0){
            Log.d("interface","Speach stuff ready to go");
        }
        else{
            Log.d("interface","Speach stuff not present");
        }
    }





    public static void startListening(Context context){
        Log.d("interface","Start Listening called");

        carpart.startVoiceRecognitionActivity(context);

    }



    private class VoiceStuff extends Activity implements TextToSpeech.OnInitListener{
        List<ResolveInfo> systemActivities=null;
private VoiceStuff(Context context){

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("interface","onCreate in voice stuff class called");

}

    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;


    public void startVoiceRecognitionActivity(Context context){


          //super.startVoiceRecognitionActivity(context);
           Log.d("Main","method called");
        Intent myCrap = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        myCrap.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        myCrap.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speak your command now");
        startActivityForResult(myCrap,VOICE_RECOGNITION_REQUEST_CODE);

       }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
           // Fill the list view with the strings the recognizer thought it could have heard
           ArrayList<String> matches = data.getStringArrayListExtra(
           RecognizerIntent.EXTRA_RESULTS);
           String stupid = matches.get(0);
           //Toast.makeText(MainRemote.this,"" + stupid, Toast.LENGTH_SHORT).show();
           if (stupid.equals("home")) {
               //getSpeech(1);
        } 
           if (stupid.equals("map")) {
               //getSpeech(2);
        } 

           //mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches));



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



       //super.onActivityResult(requestCode, resultCode, context);
public void myStuff(int requestCode, int resultCode, Intent data) {
       if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
       // Fill the list view with the strings the recognizer thought it could have heard
       ArrayList<String> matches = data.getStringArrayListExtra(
       RecognizerIntent.EXTRA_RESULTS);
       String stupid = matches.get(0);
       //Toast.makeText(MainRemote.this,"" + stupid, Toast.LENGTH_SHORT).show();


       //mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches));
       }


} //closes VoiceStuff
public void speakOut(String speakPlz) {
     Log.d("interface","speak has been called");
    //String text = txtText.getText().toString();

    tts.speak(speakPlz, TextToSpeech.QUEUE_FLUSH, null);
}

public void onInit(int status) {
    // TODO Auto-generated method stub

}
    }



    public void onInit(int status) {
        // TODO Auto-generated method stub

    }
}

当我尝试调试代码时,调试器告诉我,当执行第startActivityForResult(i,VOICE_RECOGNITION_REQUEST_CODE);行时,iVOICE_RECOGNITION_REQUEST_CODE无法解析为变量。事实上我正在使用内部类来搞乱这两个变量吗?

这里编辑的是LogCat:

    08-19 10:18:53.221: D/interface(18165): my crap:  Intent { act=android.speech.action.RECOGNIZE_SPEECH (has extras) }
08-19 10:18:53.221: D/interface(18165): VOICE_RECOGNITION_REQUEST_CODE:  1234
08-19 10:19:43.651: D/AndroidRuntime(18165): Shutting down VM
08-19 10:19:43.651: W/dalvikvm(18165): threadid=1: thread exiting with uncaught exception (group=0x41bc7300)
08-19 10:19:43.674: E/AndroidRuntime(18165): FATAL EXCEPTION: main
08-19 10:19:43.674: E/AndroidRuntime(18165): java.lang.NullPointerException
08-19 10:19:43.674: E/AndroidRuntime(18165):    at android.app.Activity.startActivityForResult(Activity.java:3351)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at android.app.Activity.startActivityForResult(Activity.java:3312)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at com.lorenjz.phoneremotefive.BlueInterface$VoiceStuff.startVoiceRecognitionActivity(BlueInterface.java:111)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at com.lorenjz.phoneremotefive.BlueInterface.startListening(BlueInterface.java:74)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at com.lorenjz.phoneremotefive.MainRemote$1.handleMessage(MainRemote.java:306)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at android.os.Looper.loop(Looper.java:137)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at android.app.ActivityThread.main(ActivityThread.java:4745)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at java.lang.reflect.Method.invokeNative(Native Method)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at java.lang.reflect.Method.invoke(Method.java:511)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-19 10:19:43.674: E/AndroidRuntime(18165):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案