Android应用程序在模拟器中不起作用

时间:2013-10-09 13:13:44

标签: android eclipse android-emulator android-logcat

我正在开发一个Android应用程序,在我点击运行后,我解锁AVD并单击菜单加载我的应用程序,我立即收到此消息:The application Pico TTS (process com.svox..pico) has stopped unexpectedly. Please try again

我的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>

这些是来自我的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);
 }});

}

那么您认为问题在哪里?!

2 个答案:

答案 0 :(得分:4)

看起来您正在创建将当前活动作为上下文传递的TTS对象。

然后你立即开始一个新的活动(我假设不要关闭OnPause中的TTS对象)

这会让系统抱怨,因为您有一个与现在非活动活动相关联的活动服务。

您应该使用Activity Lifecycle回调来设置和拆除依赖于活动的所有服务。

例如在OnResume()中设置TTS对象并在OnPause()中将其关闭

答案 1 :(得分:0)

听起来好像你有一个加载一次的广播监听器然后没有按预期发布。如果是这种情况,那么您需要分析覆盖该广播接收器的应用程序的生命周期,并确保它在之前启动的活动时关闭。如果需要,您甚至可以在创建广播时将其包围在try catch中并且不执行任何操作。因为Android将为您处理冲突,并在其他所有方法都失败时默默地失败;显然找到合适的关闭是理想的。

这就是我能从所提供的内容中推断出的一切。