android应用程序在服务启动时崩溃

时间:2013-03-30 23:34:07

标签: android text-to-speech

我正在尝试制作SmsListener但是当我尝试调用tts时,应用程序会中断

以下是听众:

package com.example.hope_1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsListener extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
        Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
        SmsMessage[] msgs = null;
        String msg_from;
        if (bundle != null){
            //---retrieve the SMS message received---
            try{
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                for(int i=0; i<msgs.length; i++){
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                    msg_from = msgs[i].getOriginatingAddress();
                    final String msgBody = msgs[i].getMessageBody();
                    Log.println(0, msg_from, msgBody);
                    Toast toast =  Toast.makeText(context, msg_from + "   " + msgBody,Toast.LENGTH_LONG);
                    toast.show();

                  //Application is crashing on line below
                    SmsSpeaker.tts.speak(msgBody,TextToSpeech.QUEUE_FLUSH, null);
                }
            }catch(Exception e){
                    Log.d("Exception caught",e.getMessage());
            }
        }
    }
}
}

以下是Sms Speaker的代码

package com.example.hope_1;

import java.util.Locale;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.Toast;

public class SmsSpeaker extends Service implements OnInitListener {

public static TextToSpeech tts;

public int onStartCommand(Intent intent,int flags,int startId){
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}
@Override
public void onInit(int status) {
    Toast toast; 
    // TODO Auto-generated method stub
    if(status == TextToSpeech.SUCCESS){
        tts.setLanguage(Locale.US);
        toast = Toast.makeText(SmsSpeaker.this, "TTS Started", Toast.LENGTH_LONG);
        toast.show();
        }
    else {
        toast = Toast.makeText(SmsSpeaker.this, TextToSpeech.ERROR, Toast.LENGTH_LONG);
        toast.show();
        toast = Toast.makeText(SmsSpeaker.this, "TTS Engine failed to start", Toast.LENGTH_LONG);
        toast.show();
    }

}


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

}

我尝试过使用LogCat,但没有帮助...... logcat是空白的

应用程序崩溃,没有任何异常

我尝试调试代码,但在调试时显示“Source not found”。我试着看其他答案,但没有多大帮助。

您能否告诉我应用程序崩溃的原因。

1 个答案:

答案 0 :(得分:0)

你没有初始化tts

public int onStartCommand(Intent intent,int flags,int startId){
super.onStartCommand(intent, flags, startId);
tts = new TextToSpeech(this, this);
return START_STICKY;

}