如何让onActivityResult()执行?

时间:2015-10-22 01:11:33

标签: android speech-recognition

我正在尝试实现语音识别并将人员的响应结果存储为我正在创建的游戏的一部分,但由于某种原因,onActivityResult()永远不会被执行。我调试了这个问题,似乎找不到为什么会发生这种情况的原因。我创建并实现了我的自定义ActionResolver接口,允许我使用android的库和LibGDX来实现此功能。

以下是我的代码:

    public class ActionResolverAndroid extends Activity implements ActionResolver {

    Handler handler;

    private Intent speechIntent;
    private boolean isCorrectResponse = false;

    static final int check = 1;

    private ArrayList<String> phrases = new ArrayList<String>();

    public ActionResolverAndroid() {
        handler = new Handler();

        phrases.add("hello");
        phrases.add("apartments");
        phrases.add("creativity");
        phrases.add("mountains");
        phrases.add("fruits");
        phrases.add("education");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Gdx.app.log("Are we here yet?", "are we here?");


        ArrayList<String> responses = data.getStringArrayListExtra((RecognizerIntent.EXTRA_RESULTS));
        for (int i = 0; i < responses.size(); i++) {
            responses.get(i).toLowerCase();
        }

        for (int i = 0; i < phrases.size(); i++) {
            if (phrases.get(i).equals(responses.get(0))) {
                System.out.println("Correct word! 1 point son!");
                Toast.makeText(getBaseContext(), "Correct!", Toast.LENGTH_SHORT).show();
                isCorrectResponse(true);
            } else if (!isCorrectResponse && i == phrases.size() - 1) {
                Toast.makeText(getBaseContext(), "Wrong!", Toast.LENGTH_SHORT).show();
                System.out.println("Wrong word!");

            }
        }

    }

    @Override
    public void implementVoiceRecognizer() {
        handler.post(new Runnable() {
            public void run() {
                speechIntent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

                speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                speechIntent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        "Say the phrase!");

                startActivityForResult(speechIntent, check);



            }
    });

    }

    @Override
    public void isCorrectResponse(boolean b) {
                isCorrectResponse = b;

    }

    public boolean isCorrectResponse() {
        return isCorrectResponse;
    }

    // public void setIsCorrectResponse(boolean b){
    // isCorrectResponse = b;
    // }

    public Intent getSpeechIntent() {
        return speechIntent;
    }

}

这是我在平板电脑上运行游戏时打印到LogCat的错误:

10-22 15:39:02.091: E/AndroidRuntime(15248): FATAL EXCEPTION: main
10-22 15:39:02.091: E/AndroidRuntime(15248): Process: com.mygdx.Dextero.android, PID: 15248
10-22 15:39:02.091: E/AndroidRuntime(15248): java.lang.NullPointerException
10-22 15:39:02.091: E/AndroidRuntime(15248):    at android.app.Activity.startActivityForResult(Activity.java:3457)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at android.app.Activity.startActivityForResult(Activity.java:3418)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at com.mygdx.Dextero.android.ActionResolverAndroid$1.run(ActionResolverAndroid.java:74)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at android.os.Handler.handleCallback(Handler.java:808)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at android.os.Handler.dispatchMessage(Handler.java:103)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at android.os.Looper.loop(Looper.java:193)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at android.app.ActivityThread.main(ActivityThread.java:5292)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at java.lang.reflect.Method.invokeNative(Native Method)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at java.lang.reflect.Method.invoke(Method.java:515)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
10-22 15:39:02.091: E/AndroidRuntime(15248):    at dalvik.system.NativeStart.main(Native Method)
10-22 15:39:02.826: V/SurfaceView(15248): com.badlogic.gdx.backends.android.surfaceview.GLSurfaceView20{41c53d30 VFE..... .F....I. 0,0-1280,736} got resized: w=1280 h=736, cur w=-1 h=-1

1 个答案:

答案 0 :(得分:2)

替换它:

((Activity) context).startActivityForResult(speechIntent, check);

只有这个:

startActivityForResult(speechIntent, check);

您正在从另一个Activity调用Result调用,而当前的调用没有收到它。

此外,不需要在Activity的构造函数中传递Context对象,因为每个Activity都是Context本身: - )