语音识别不起作用

时间:2012-09-01 18:10:36

标签: android voice-recognition android-logcat

我使用现有的语音识别示例代码。但是这个代码在我的eclipse中不起作用。我的logcat中出现了以下错误。但这段代码完全在别人工作。我提到了很多教程,但我无法清楚我犯了错误?

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

1 个答案:

答案 0 :(得分:1)

请检查布局R.id.speakButton中是否存在activity_main.xml。如果不存在,findviewbyid将返回null。