为什么我不能用语音识别开始我的活动?

时间:2013-08-26 13:47:48

标签: android

出于某种原因,我的搜索活动正在运行,但thumbsup活动无法启动。它承认我说“三”然后我的应用程序崩溃。我需要在这里添加什么才能使其正常工作?

在添加此部分之前,应用程序运行良好:

if (textMatchList.get(0).contains("three")) {
                     // create an intent indicating we want
                        // to start the ThumbsUp activity.
                        // Important! Make sure your activity is
                        // in the AndroidManifest.xml file!
                        Intent i = new Intent(this, ThumbsUp.class);

                        // start the activity based on the Intent
                        startActivity(i);

logcat的:

08-26 09:58:14.934: E/Trace(7730): error opening trace file: No such file or directory (2)
08-26 09:58:14.934: D/ActivityThread(7730): setTargetHeapUtilization:0.25
08-26 09:58:14.934: D/ActivityThread(7730): setTargetHeapIdealFree:8388608
08-26 09:58:14.934: D/ActivityThread(7730): setTargetHeapConcurrentStart:2097152
08-26 09:58:15.214: D/libEGL(7730): loaded /system/lib/egl/libEGL_adreno200.so
08-26 09:58:15.244: D/libEGL(7730): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
08-26 09:58:15.244: D/libEGL(7730): loaded /system/lib/egl/libGLESv2_adreno200.so
08-26 09:58:15.605: I/Adreno200-EGLSUB(7730): <ConfigWindowMatch:2087>: Format RGBA_8888.
08-26 09:58:15.695: E/(7730): <s3dReadConfigFile:75>: Can't open file for reading
08-26 09:58:15.695: E/(7730): <s3dReadConfigFile:75>: Can't open file for reading
08-26 09:58:15.695: D/OpenGLRenderer(7730): Enabling debug mode 0
08-26 09:58:15.745: I/Choreographer(7730): Skipped 31 frames!  The application may be doing too much work on its main thread.

Java代码:

package com.example.voicerecognitionactivity;

import java.util.ArrayList;
import java.util.List;

import com.example.voicerecognitionactivity.ThumbsUp;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class VoiceRecognitionActivity extends Activity {
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;

    private EditText metTextHint;
    private ListView mlvTextMatches;
    private Spinner msTextMatches;
    private Button mbtSpeak;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        metTextHint = (EditText) findViewById(R.id.etTextHint);
        mlvTextMatches = (ListView) findViewById(R.id.lvTextMatches);
        msTextMatches = (Spinner) findViewById(R.id.sNoOfMatches);
        mbtSpeak = (Button) findViewById(R.id.btSpeak);
    }

    public void checkVoiceRecognition() {
        // Check if voice recognition is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() == 0) {
            mbtSpeak.setEnabled(false);
            Toast.makeText(this, "Voice recognizer not present",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void speak(View view) {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        // Specify the calling package to identify your application
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
                .getPackage().getName());

        // Display an hint to the user about what he should say.
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, metTextHint.getText()
                .toString());

        // Given an hint to the recognizer about what the user is going to say
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

        // If number of Matches is not selected then return show toast message
        if (msTextMatches.getSelectedItemPosition() == AdapterView.INVALID_POSITION) {
            Toast.makeText(this, "Please select No. of Matches from spinner",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        int noOfMatches = Integer.parseInt(msTextMatches.getSelectedItem()
                .toString());
        // Specify how many results you want to receive. The results will be
        // sorted where the first result is the one with higher confidence.

        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, noOfMatches);

        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

            //If Voice recognition is successful then it returns RESULT_OK
            if(resultCode == RESULT_OK) {

                ArrayList<String> textMatchList = data
                .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                if (!textMatchList.isEmpty()) {

                    if (textMatchList.get(0).contains("three")) {
                     // create an intent indicating we want
                        // to start the ThumbsUp activity.
                        // Important! Make sure your activity is
                        // in the AndroidManifest.xml file!
                        Intent i = new Intent(this, ThumbsUp.class);

                        // start the activity based on the Intent
                        startActivity(i);

                    // If first Match contains the 'search' word
                    // Then start web search.
                    if (textMatchList.get(0).contains("search")) {

                        String searchQuery = textMatchList.get(0).replace("search",
                        " ");
                        Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
                        search.putExtra(SearchManager.QUERY, searchQuery);
                        startActivity(search);

                    } else {
                        // populate the Matches
                        mlvTextMatches
                        .setAdapter(new ArrayAdapter<String>(this,
                                android.R.layout.simple_list_item_1,
                                textMatchList));
                    }

                }
            //Result code for various error.   
            }else if(resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){
                showToastMessage("Audio Error");
            }else if(resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){
                showToastMessage("Client Error");
            }else if(resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){
                showToastMessage("Network Error");
            }else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){
                showToastMessage("No Match");
            }else if(resultCode == RecognizerIntent.RESULT_SERVER_ERROR){
                showToastMessage("Server Error");
            }
        super.onActivityResult(requestCode, resultCode, data);}
    }
    void showToastMessage(String message){
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

清单代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.voicerecognitionactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.voicerecognitionactivity.VoiceRecognitionActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.example.VoiceRecognitionActivity.ThumbsUp"
                  android:label ="@string/thumbsup"
            ></activity>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

您是否已在清单文件中添加此内容:

   <Activity
         android:name =  "Packagename.class"
         android:label = "@string/label"
     >

   </Activity>