Ram
Ram

Reputation: 1697

voice recognition not working

I used voice recognition sample code from existing one.But this code not working in my eclipse.I got following error in my logcat.But this code perfectly working in others.I refered so many tutorials but i can't clear where i put mistake?

Mycode:

public class MainActivity extends Activity {
     private static final int REQUEST_CODE = 1234;
        private ListView wordsList;


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

        Button speakButton = (Button) findViewById(R.id.speakButton);

        wordsList = (ListView) findViewById(R.id.list);

        // Disable button if no recognition service is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() == 0)
        {
            speakButton.setEnabled(false);
            speakButton.setText("Recognizer not present");
        }
    }

    /**
     * Handle the action of the button being clicked
     */
    public void speakButtonClicked(View v)
    {
        startVoiceRecognitionActivity();
    }

    /**
     * Fire an intent to start the voice recognition activity.
     */
    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_PROMPT, "Voice recognition Demo...");
        startActivityForResult(intent, REQUEST_CODE);
    }

    /**
     * Handle the results from the voice recognition activity.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
        {
            // Populate the wordsList with the String values the recognition engine thought it heard
            ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                    matches));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

Logcat:

09-01 23:29:39.441: E/AndroidRuntime(225): Uncaught handler: thread main exiting due to uncaught exception
09-01 23:29:39.580: E/AndroidRuntime(225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.voicerecog/com.example.voicerecog.MainActivity}: java.lang.NullPointerException
09-01 23:29:39.580: E/AndroidRuntime(225):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
09-01 23:29:39.580: E/AndroidRuntime(225):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
09-01 23:29:39.580: E/AndroidRuntime(225):  at android.app.ActivityThread.access$2100(ActivityThread.java:116)
09-01 23:29:39.580: E/AndroidRuntime(225):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
09-01 23:29:39.580: E/AndroidRuntime(225):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 23:29:39.580: E/AndroidRuntime(225):  at android.os.Looper.loop(Looper.java:123)
09-01 23:29:39.580: E/AndroidRuntime(225):  at android.app.ActivityThread.main(ActivityThread.java:4203)
09-01 23:29:39.580: E/AndroidRuntime(225):  at java.lang.reflect.Method.invokeNative(Native Method)
09-01 23:29:39.580: E/AndroidRuntime(225):  at java.lang.reflect.Method.invoke(Method.java:521)
09-01 23:29:39.580: E/AndroidRuntime(225):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-01 23:29:39.580: E/AndroidRuntime(225):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
09-01 23:29:39.580: E/AndroidRuntime(225):  at dalvik.system.NativeStart.main(Native Method)
09-01 23:29:39.580: E/AndroidRuntime(225): Caused by: java.lang.NullPointerException
09-01 23:29:39.580: E/AndroidRuntime(225):  at com.example.voicerecog.MainActivity.onCreate(MainActivity.java:38)
09-01 23:29:39.580: E/AndroidRuntime(225):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-01 23:29:39.580: E/AndroidRuntime(225):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
09-01 23:29:39.580: E/AndroidRuntime(225):  ... 11 more

Upvotes: 0

Views: 530

Answers (1)

nandeesh
nandeesh

Reputation: 24820

Do check if R.id.speakButton is present in the layout activity_main.xml. If it is not present findviewbyid returns null.

Upvotes: 1

Related Questions