Android的文字转语音应用空指针异常 [英] Android TextToSpeech app null pointer exception
问题描述
我得到在Android开发者控制台一个空指针异常报告。我需要一些建议,以什么可能是这里的问题,堆栈跟踪是这样的。
I am getting a null pointer exception report in the android developer console. I need some advise as to what possibly is the problem here, the stack trace is like this
java.lang.NullPointerException
at com.myfreeapp.workers.Speaker.onInit(Speaker.java:57)
at android.speech.tts.TextToSpeech$1.onServiceConnected(TextToSpeech.java:451)
at android.app.ActivityThread$PackageInfo$ServiceDispatcher.doConnected(ActivityThread.java:1247)
at android.app.ActivityThread$PackageInfo$ServiceDispatcher$RunConnection.run(ActivityThread.java:1264)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4668)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
在我的应用程序相关的code段是
The relevant code snippet in my app is
public Speaker(final Context context, final Settings settings)
{
this.settings = settings;
params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "myfreeapps");
tts= new TextToSpeech(context, this);
Utils.log(TAG, "Created TextToSpeech..");
}
@Override
public void onInit(final int status)
{
Utils.log(TAG, "TTS onInit..");
//below is line 57 mentioned in the stack trace
tts.setOnUtteranceCompletedListener(new SpeechFinishedListener());
tts.setLanguage(Locale.getDefault());
tts.setSpeechRate(settings.getSpeed());
tts.setPitch(settings.getPitch());
ready = true;
}
请首先我需要明确到底什么是空..
指向变量TTS上线57堆栈跟踪为空..?
Please first of all I need to be clear what exactly is null.. Is the stack trace pointing to variable tts on line 57 to be null..?
要么是空指针异常文字转语音方法setOnUtteranceCompletedListener内部发生的?
Or is the null pointer exception happening inside the TextToSpeech method setOnUtteranceCompletedListener ?
议长实例在粘服务主线程上创建的,当我调试我的code从文字转语音回调也回来在同一线程上。
The Speaker instance is created on the main thread in a sticky service, and when I debug my code the callback from TextToSpeech also comes back on the same thread..
我不明白怎么可能变量TTS为空?
I don't understand how could the variable tts be null ???
这个问题是不是在我结束重现的方式。我有这个堆栈strace的报道,开发者控制台上几次。
By the way this problem is not reproducible on my end. I have this stack strace reported several times on the developer console.
请指教,
推荐答案
我解决了这个问题,下面是它的工作原理
I resolved the problem, here is how it worked
有,我是在一个非UI线程创建议长对象的情况。
回调的OnInit()会来的UI线程/主线程。
There was a case where I was creating Speaker object on a non-ui thread. The callback to onInit() would come on UI thread/main thread..
现在即使运行它可能并不总是崩溃,但我意识到这是不是安全,空指针异常的几率有..
now even though running it might not always crash but I realised it was not safe and chances of null pointer exceptions are there..
所以我的更新code看起来像这样
so my updated code looks like this
public Speaker(final Context context, final Settings settings)
{
this.settings = settings;
params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "myfreeapps");
synchronize(synchobject)
{
tts= new TextToSpeech(context, this);
}
Utils.log(TAG, "Created TextToSpeech..");
}
@Override
public void onInit(final int status)
{
Utils.log(TAG, "TTS onInit..");
synchronize(synchobject)
{
tts.setOnUtteranceCompletedListener(new SpeechFinishedListener());
tts.setLanguage(Locale.getDefault());
tts.setSpeechRate(settings.getSpeed());
tts.setPitch(settings.getPitch());
ready = true;
}
}
这似乎已经解决我的问题,感谢每一位对你的贡献来帮助我,..
This seems to have resolve my problem , thanks every one for your contribution to help me out,..
这篇关于Android的文字转语音应用空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!