java - 语音识别 Android NPE

标签 java android eclipse nullpointerexception speech-recognition

我的 Android 应用程序出现问题。问题是语音识别在到达方法 startActivityForResult (intent, int) 时不起作用,它表明我必须添加 Bundle。语音识别被添加到一个类中,该类在 Adaper 类中调用,而不是像人们那样在 MainActivity 中调用。

这是语音识别的代码

public class SpeechRecog extends Activity {

    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
    ArrayList<String> thingsYouSaid = new ArrayList<String>();

    public void Start(Intent i, Context c) {
        i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
        i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Are You Done ,  yet? ");
        //try{
        startActivityForResult(i, VOICE_RECOGNITION_REQUEST_CODE);
        //}catch(Exception e){
        //Toast.makeText(c, "SpeechRecogntion is not avalible on your device ", Toast.LENGTH_LONG).show();

        //}

    }


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

            thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            if (checkResult())
                Toast.makeText(getApplicationContext(), 
                        "You are done ", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(getApplicationContext(), 
                        "You are not done ", Toast.LENGTH_LONG).show();

        }

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


    public boolean checkResult() {

        for (int i = 0; i < thingsYouSaid.size(); i++) {

            if ((thingsYouSaid.get(i).equals("done")) 
                    || (thingsYouSaid.get(i).equals("yes")))
                return true;

        }
        //if (textResult.equals("done")||(textResult.equals("yes")))
        //return true ; 

        return false;
    }

}

这里是适配器类中的 OnclickListenre

Lh.btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        String[] items = db.GetItemArray(Lh.text);
        String[] itemFlags = db.GetItemsFlagsArray(Lh.text);
        //final itemHolder ihc = new itemHolder();

        if (Lh.btn.getText().equals("read")) {
            Lh.btn.setText("Pause");
            int i;
            for (i = 0; i < items.length; i++) {

                if (Integer.valueOf(itemFlags[i]) == 1) {

                    continue;
                }

                int timeofitem = Integer.parseInt(db.getItemTime(items[i]));
                //Toast.makeText(context, items[i] , Toast.LENGTH_SHORT).show();
                TTS.Talk(items[i], 1);
                TTS.Scilence(timeofitem);

                SpeechRecog SP = new SpeechRecog();
                SP.Start(new Intent(), context);

                // checking  items
                ItemsFields itemToBeChanged = new ItemsFields(Lh.text, items[i], 0, timeofitem);
                itemToBeChanged.setFlag(1);
                db.updateItems(itemToBeChanged);

                //ihc.checkbox.setChecked(true); 
            }

            TTS.Talk("Congrats ,Your Checklist is Completed ", 1);


        } else {
            Lh.btn.setText("read");
            TTS.Talk("The Checklist is paused , Press again if you want to continue , Bye", 0);
            return;

        }
    }

});

这是调试所说的

Thread [<1> main] (Suspended (exception NullPointerException))  
        <VM does not provide monitor information>   
        SpeechRecog(Activity).startActivityForResult(Intent, int, Bundle) line: 3464    
        SpeechRecog(Activity).startActivityForResult(Intent, int) line: 3425    
        SpeechRecog.Start(Intent, Context) line: 24 
        ViewAdapter$1.onClick(View) line: 140   
        Button(View).performClick() line: 4475  
        View$PerformClick.run() line: 18786 
        Handler.handleCallback(Message) line: 730   
        ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 92 
        Looper.loop() line: 176 
        ActivityThread.main(String[]) line: 5419    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 525  
        ZygoteInit$MethodAndArgsCaller.run() line: 1046 
        ZygoteInit.main(String[]) line: 862 
        NativeStart.main(String[]) line: not available [native method]  

调试后,它显示了这个奇怪的代码并突出显示 * <var>flagsMask</var>

/**
 * Like {@link #startActivityForResult(Intent, int)}, but allowing you
 * to use a IntentSender to describe the activity to be started.  If
 * the IntentSender is for an activity, that activity will be started
 * as if you had called the regular {@link #startActivityForResult(Intent, int)}
 * here; otherwise, its associated action will be executed (such as
 * sending a broadcast) as if you had called
 * {@link IntentSender#sendIntent IntentSender.sendIntent} on it.
 *
 * @param intent       The IntentSender to launch.
 * @param requestCode  If >= 0, this code will be returned in
 *                     onActivityResult() when the activity exits.
 * @param fillInIntent If non-null, this will be provided as the
 *                     intent parameter to {@link IntentSender#sendIntent}.
 * @param flagsMask    Intent flags in the original IntentSender that you
 *                     would like to change.
 * @param flagsValues  Desired values for any bits set in
 *                     <var>flagsMask</var>
 * @param extraFlags   Always set to 0.
 * @param options      Additional options for how the Activity should be started.
 *                     See {@link android.content.Context#startActivity(Intent, Bundle)
 *                     Context.startActivity(Intent, Bundle)} for more details.  If options
 *                     have also been supplied by the IntentSender, options given here will
 *                     override any that conflict with those given by the IntentSender.
 */
public void startIntentSenderForResult(IntentSender intent, int requestCode,
                                       Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
                                       Bundle options) throws IntentSender.SendIntentException {
    if (mParent == null) {
        startIntentSenderForResultInner(intent, requestCode, fillInIntent,
                flagsMask, flagsValues, this, options);
    } else if (options != null) {
        mParent.startIntentSenderFromChild(this, intent, requestCode,
                fillInIntent, flagsMask, flagsValues, extraFlags, options);
    } else {
        // Note we want to go through this call for compatibility with
        // existing applications that may have overridden the method.
        mParent.startIntentSenderFromChild(this, intent, requestCode,
                fillInIntent, flagsMask, flagsValues, extraFlags);
    }
}

这是日志猫所说的

05-04 23:44:00.845: D/libEGL(24183): loaded /system/lib/egl/libEGL_mali.so
05-04 23:44:00.850: D/libEGL(24183): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-04 23:44:00.855: D/libEGL(24183): loaded /system/lib/egl/libGLESv2_mali.so
05-04 23:44:00.865: E/(24183): Device driver API match
05-04 23:44:00.865: E/(24183): Device driver API version: 23
05-04 23:44:00.865: E/(24183): User space API version: 23 
05-04 23:44:00.865: E/(24183): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct  9 21:05:57 KST 2013 
05-04 23:44:00.970: D/OpenGLRenderer(24183): Enabling debug mode 0
05-04 23:44:01.020: D/AbsListView(24183): unregisterIRListener() is called 
05-04 23:44:01.045: D/AbsListView(24183): unregisterIRListener() is called 
05-04 23:44:01.080: D/AbsListView(24183): unregisterIRListener() is called 
05-04 23:44:01.170: D/AbsListView(24183): unregisterIRListener() is called 
05-04 23:44:02.630: D/AbsListView(24183): Get MotionRecognitionManager
05-04 23:44:02.785: W/ResourceType(24183): Invalid package identifier when getting bag for resource number 0xffffffff
05-04 23:44:02.795: W/ResourceType(24183): Invalid package identifier when getting bag for resource number 0xffffffff
05-04 23:44:02.805: W/ResourceType(24183): Invalid package identifier when getting bag for resource number 0xffffffff
05-04 23:44:02.810: W/ResourceType(24183): Invalid package identifier when getting bag for resource number 0xffffffff

最佳答案

切勿使用new实例化 Activity 。

使用 Intent 启动新 Activity 或使用框架为 onActivityResult() 实例化的现有 Activity 。

对于后者,替换

SpeechRecog SP = new SpeechRecog();
SP.Start(new Intent(), context);

使用 Start() 中的代码(即设置 Intent 并调用 startActivityForResult() 并拉取 onActivityResult() 到该 Activity 。

关于java - 语音识别 Android NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23460208/

相关文章:

java - 2D Array_List 如何工作?

java - Eclipse 插件安装问题

java - 如何将该项目导入到 Eclipse 中?

java - 无法编写 X_path 以从应用程序中的应用程序相关报告获取文本,

java - Android SQLite 从空表中获取列列表

android - Android 上的即时消息?

android - 无法从 Eclipse Android SDK 中的属性调出 "Reference Chooser"窗口

android - 我应该使用什么设备唯一 ID 作为 key 来加密某些值

java - 使用rest webservices打开数据库连接并从数据库获取数据

java - 如何在ReSTLet Framework中设置ClientResource发送请求的超时时间