我想创建一个应用程序,当我收到短信时,可以用TextToSpeach说出一个文本。
为此,我有一个服务,一个接收器和一个类SaidThis。
请参阅我的代码:
我的服务:
package fr.dabernat.smsreceiver;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Service extends IntentService {
private Handler handler;
public static String text;
public Service() {
super("Service");
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (!extras.isEmpty()) {
SaidThis test = new SaidThis();
test.SpeakText();
//showNotification();
//SaidThis test = new SaidThis(this);
//test.text("J'ai honte de moi");
}
Receiver.completeWakefulIntent(intent);
}
}
我的接收者:
package fr.dabernat.smsreceiver;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class Receiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that Service will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
Service.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
我的班级SaidThis:
package fr.dabernat.smsreceiver;
import android.speech.tts.TextToSpeech;
import java.util.Locale;
/**
* Created by Damien on 19/09/2014.
*/
public class SaidThis extends Service implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {
TextToSpeech tts;
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.FRANCE);
}
}
@Override
public void onUtteranceCompleted(String s) {
stopSelf();
}
public void SpeakText(){
tts.speak(text,TextToSpeech.QUEUE_FLUSH, null);
}
}
我的Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.dabernat.smsreceiver" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Receiver" >
<intent-filter>
<!-- A CHANGER POUR "ECOUTER" UN AUTRE BROADCAST -->
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service android:name=".Service" />
</application>
</manifest>
错误:
10-08 10:44:51.980 1371-1371/fr.dabernat.smsreceiver E/dalvikvm﹕ >>>>> Normal User
10-08 10:44:51.980 1371-1371/fr.dabernat.smsreceiver E/dalvikvm﹕ >>>>> fr.dabernat.smsreceiver [ userId:0 | appId:10309 ]
10-08 10:44:52.080 1371-1412/fr.dabernat.smsreceiver E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[Service]
Process: fr.dabernat.smsreceiver, PID: 1371
java.lang.NullPointerException
at fr.dabernat.smsreceiver.SaidThis.SpeakText(SaidThis.java:34)
at fr.dabernat.smsreceiver.Service.onHandleIntent(Service.java:36)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.os.HandlerThread.run(HandlerThread.java:61)
我认为问题来自我的服务,但我不知道在哪里。 我之前尝试在网上找到一些答案,但我找不到解决问题的方法。
答案 0 :(得分:1)
您的tts
对象未在任何地方初始化,因此它是null
。
在tts = new TextToSpeech()
方法中使用它之前,您必须在某处初始化它:SpeakText()
。
我认为onInit()
方法非常适合初始化对象。