我正在开发一个 android 应用程序,在我点击运行后,我解锁 AVD 并单击菜单加载我的应用程序,我立即收到此消息:应用程序 Pico TTS(进程 com.svox..pico ) 意外停止。请重试
这是我的 AndroidManifest.xml
:
<application android:icon="@drawable/icon" android:label="@string/app_name" android:allowBackup="true">
<activity android:name=".MPActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这些是来 self 的 logcat 的消息:
10-09 13:01:14.796: I/TextToSpeech.java(1861): initTts() successfully bound to service
10-09 13:01:16.916: E/ActivityThread(1861): Activity com.kelamrsan.MPActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$1@4051d710 that was originally bound here
10-09 13:01:16.916: E/ActivityThread(1861): android.app.ServiceConnectionLeaked: Activity com.kelamrsan.MPActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$1@4051d710 that was originally bound here
10-09 13:01:16.916: E/ActivityThread(1861): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:938)
10-09 13:01:16.916: E/ActivityThread(1861): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:833)
10-09 13:01:16.916: E/ActivityThread(1861): at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
10-09 13:01:16.916: E/ActivityThread(1861): at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:467)
10-09 13:01:16.916: E/ActivityThread(1861): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:433)
10-09 13:01:16.916: E/ActivityThread(1861): at com.kelamrsan.MPActivity.onCreate(MPActivity.java:47)
10-09 13:01:16.916: E/ActivityThread(1861): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-09 13:01:16.916: E/ActivityThread(1861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-09 13:01:16.916: E/ActivityThread(1861): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-09 13:01:16.916: E/ActivityThread(1861): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-09 13:01:16.916: E/ActivityThread(1861): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-09 13:01:16.916: E/ActivityThread(1861): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 13:01:16.916: E/ActivityThread(1861): at android.os.Looper.loop(Looper.java:130)
10-09 13:01:16.916: E/ActivityThread(1861): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-09 13:01:16.916: E/ActivityThread(1861): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 13:01:16.916: E/ActivityThread(1861): at java.lang.reflect.Method.invoke(Method.java:507)
10-09 13:01:16.916: E/ActivityThread(1861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-09 13:01:16.916: E/ActivityThread(1861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-09 13:01:16.916: E/ActivityThread(1861): at dalvik.system.NativeStart.main(Native Method)
这是我的 onCreate
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
textView = (TextView) findViewById(R.id.textView);
textViewCol = (TextView) findViewById(R.id.textViewColor);
targetImage = (ImageView) findViewById(R.id.targetimage);
textViewVal = (TextView) findViewById(R.id.textViewValue);
mTts = new TextToSpeech(this, new OnInitListener() {
@Override
public void onInit(int status) {
TTSInitialized = true;
}
});
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}});
}
那么你认为问题出在哪里呢?!
最佳答案
看起来您正在创建将当前 Activity 作为上下文传递的 TTS 对象。
然后您立即开始一个新 Activity (我假设不要在您的 OnPause 中关闭 TTS 对象)
这会使系统提示,因为您有一个 Activity 的服务与现在不活动的 Activity 相关联。
你应该使用 Activity Lifecycle设置和拆除任何依赖于 Activity 的服务的回调。
例如在 OnResume() 中设置您的 TTS 对象并在 OnPause() 中关闭它
关于Android 应用程序无法在模拟器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19273102/