android - MainActivity 泄露了最初绑定(bind)在这里的 ServiceConnection android.speech.SpeechRecognizer$Connection@414ee400

标签 android speech-recognition serviceconnection

在我的应用程序中,我识别出用户说“退出”或“关闭”,应用程序应该关闭。 使用此代码

SpeechRecognizer sr;
Map<String, Integer> dictionary;
private static final int EXIT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    populateDictionary();
    SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);
    sr.setRecognitionListener(this);
    Intent voiceIntent = RecognizerIntent.getVoiceDetailsIntent(getApplicationContext());
    sr.startListening(voiceIntent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private void populateDictionary() {
    dictionary = new HashMap<String, Integer>();
    dictionary.put("exit", EXIT);
    dictionary.put("close", EXIT);
}

@Override
public void onResults(Bundle results) {
    ArrayList<String> strList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    for (int i = 0; i < strList.size();i++ ) {
        String sentence = strList.get(i).toLowerCase(Locale.getDefault());
        Integer operation = dictionary.get(sentence);
        if(operation != null){
            switch(operation){
                case EXIT:{
                    this.finish();
                };break;
            }
        }
    }
} 

我在语音识别后得到这个运行时错误。

11-12 15:38:05.351: E/Trace(14934): error opening trace file: No such file or directory (2)
11-12 15:38:05.511: D/libEGL(14934): loaded /system/lib/egl/libEGL_mali.so
11-12 15:38:05.521: D/libEGL(14934): loaded /system/lib/egl/libGLESv1_CM_mali.so
11-12 15:38:05.521: D/libEGL(14934): loaded /system/lib/egl/libGLESv2_mali.so
11-12 15:38:05.541: D/OpenGLRenderer(14934): Enabling debug mode 0
11-12 15:38:08.884: E/ActivityThread(14934): Activity com.example.voicetest01.MainActivity has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@414f0e40 that was originally bound here
11-12 15:38:08.884: E/ActivityThread(14934): android.app.ServiceConnectionLeaked: Activity com.example.voicetest01.MainActivity has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@414f0e40 that was originally bound here
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:965)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:859)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.ContextImpl.bindService(ContextImpl.java:1214)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.ContextImpl.bindService(ContextImpl.java:1206)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.content.ContextWrapper.bindService(ContextWrapper.java:394)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.speech.SpeechRecognizer.startListening(SpeechRecognizer.java:281)
11-12 15:38:08.884: E/ActivityThread(14934):    at com.example.voicetest01.MainActivity.onCreate(MainActivity.java:32)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.Activity.performCreate(Activity.java:5008)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.os.Looper.loop(Looper.java:137)
11-12 15:38:08.884: E/ActivityThread(14934):    at android.app.ActivityThread.main(ActivityThread.java:4744)
11-12 15:38:08.884: E/ActivityThread(14934):    at java.lang.reflect.Method.invokeNative(Native Method)
11-12 15:38:08.884: E/ActivityThread(14934):    at java.lang.reflect.Method.invoke(Method.java:511)
11-12 15:38:08.884: E/ActivityThread(14934):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 15:38:08.884: E/ActivityThread(14934):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 15:38:08.884: E/ActivityThread(14934):    at dalvik.system.NativeStart.main(Native Method)

如果我尝试破坏 SpeechRecognizer,我会收到另一个运行时错误,因为 sr 为 null。

@Override
public void onDestroy(){
    sr.destroy();
    super.onDestroy();
}

日志

11-12 15:29:24.383: E/Trace(13724): error opening trace file: No such file or directory (2)
11-12 15:29:24.633: D/libEGL(13724): loaded /system/lib/egl/libEGL_mali.so
11-12 15:29:24.633: D/libEGL(13724): loaded /system/lib/egl/libGLESv1_CM_mali.so
11-12 15:29:24.643: D/libEGL(13724): loaded /system/lib/egl/libGLESv2_mali.so
11-12 15:29:24.673: D/OpenGLRenderer(13724): Enabling debug mode 0
11-12 15:29:29.047: D/AndroidRuntime(13724): Shutting down VM
11-12 15:29:29.047: W/dalvikvm(13724): threadid=1: thread exiting with uncaught exception (group=0x40ccf318)
11-12 15:29:29.047: E/AndroidRuntime(13724): FATAL EXCEPTION: main
11-12 15:29:29.047: E/AndroidRuntime(13724): java.lang.NullPointerException
11-12 15:29:29.047: E/AndroidRuntime(13724):    at com.example.voicetest01.MainActivity.onResults(MainActivity.java:77)
11-12 15:29:29.047: E/AndroidRuntime(13724):    at android.speech.SpeechRecognizer$InternalListener$1.handleMessage(SpeechRecognizer.java:442)
11-12 15:29:29.047: E/AndroidRuntime(13724):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 15:29:29.047: E/AndroidRuntime(13724):    at android.os.Looper.loop(Looper.java:137)
11-12 15:29:29.047: E/AndroidRuntime(13724):    at android.app.ActivityThread.main(ActivityThread.java:4744)
11-12 15:29:29.047: E/AndroidRuntime(13724):    at java.lang.reflect.Method.invokeNative(Native Method)
11-12 15:29:29.047: E/AndroidRuntime(13724):    at java.lang.reflect.Method.invoke(Method.java:511)
11-12 15:29:29.047: E/AndroidRuntime(13724):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 15:29:29.047: E/AndroidRuntime(13724):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 15:29:29.047: E/AndroidRuntime(13724):    at dalvik.system.NativeStart.main(Native Method)
11-12 15:29:31.500: I/Process(13724): Sending signal. PID: 13724 SIG: 9

我该怎么办?

最佳答案

我认为问题可能出在线上:

sr.destroy();

如果 sr 为空,你会得到 NullPointerException,并且

super.onDestroy();

Dont 已被调用。尝试下一步:

if(sr!=null)
{
    sr.destroy();
}

或:

try{
        sr.destroy();
} 
 catch (Exception e)
{
 Log.e(TAG,"Exception:"+e.toString());
}

关于android - MainActivity 泄露了最初绑定(bind)在这里的 ServiceConnection android.speech.SpeechRecognizer$Connection@414ee400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19930999/

相关文章:

Java 谷歌语音识别 API

android - 如何在 Android 设备中运行语音识别(语音到文本)?

Android ServiceConnection - EBADF(错误文件编号)

java - 用于 ServiceConnection 回调的线程 (Android)

android - 每个服务绑定(bind)是否需要一个 ServiceConnection?

java - Note5读取Mifare经典卡UID

java - 复选框 true/false 表示 ischecked 工作,但不显示 ui 复选标记

java - fragment 翻译和电话方向错误

java - 在后台运行应用程序

speech-recognition - 语音检测和判别的开源代码