Android 为什么文本到语音(TTS)对象即使在初始化后也会变为null?
我在使用Android 为什么文本到语音(TTS)对象即使在初始化后也会变为null?,android,kotlin,nullpointerexception,text-to-speech,Android,Kotlin,Nullpointerexception,Text To Speech,我在使用tts变量时遇到问题。即使文本到语音对象已初始化,它也将变为null 在ShowLabel()函数中,我想对firstObject变量的文本使用ttsShowLabel()函数接收FirebaseVisionImageLabel对象的列表。我在说出ShowLabel()中的文本时遇到问题。尽管控制台将布尔值记录为true,并且speak()内部OnInit()工作 我已经设置了一个checkerttsIsInitialized,它是布尔类型的,因此在showLabel()上运行speak
tts
变量时遇到问题。即使文本到语音对象已初始化,它也将变为null
在ShowLabel()
函数中,我想对firstObject
变量的文本使用tts
ShowLabel()
函数接收FirebaseVisionImageLabel
对象的列表。我在说出ShowLabel()
中的文本时遇到问题。尽管控制台将布尔值记录为true,并且speak()
内部OnInit()
工作
我已经设置了一个checkerttsIsInitialized
,它是布尔类型的,因此在showLabel()
上运行speak()
之前,它必须为true。但它给了我零。任何帮助都会很好。如果有最好的方法,请告诉我
因此,现在我的问题是tts
内部Showlabel()
总是变为空。有人能指出我的错误在哪里吗
这是FirebaseQuickStart上的一个项目
LivePreviewActivity
class LivePreviewActivity : AppCompatActivity(), OnRequestPermissionsResultCallback, TextToSpeech.OnInitListener {
private var cameraSource: CameraSource? = null
private var tts: TextToSpeech? = null
var firstObject = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_live_preview)
tts = TextToSpeech(this, this)
}
fun showLabel(labels: List<FirebaseVisionImageLabel>?) {
if (!ttsIsInitialized) {
Log.d(TAG, "TTS not Initialized")
Log.d("Boolean", ttsIsInitialized.toString())
} else {
firstObject = labels!!.first().text
tts!!.speak(firstObject, TextToSpeech.QUEUE_FLUSH, null, "")
Log.d("Boolean", ttsIsInitialized.toString())
}
}
override fun onInit(status: Int) {
if (status == TextToSpeech.SUCCESS) {
val result = tts!!.setLanguage(Locale.US)
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS","The Language specified is not supported!")
} else {
Log.d("TTS", "Initilization Successful")
tts!!.speak("This is a test", TextToSpeech.QUEUE_FLUSH, null, "")
cameraSource?.setMachineLearningFrameProcessor(ImageLabelingProcessor())
ttsIsInitialized = true
}
} else {
Log.e("TTS", "Initilization Failed!")
}
}
是否有可能在OnCreate之前执行onInit()
?@TejasPandya我不确定。有没有办法看它是否先执行?有可能延迟超控功能吗?您好!你能提供航海日志吗?另外,您从侦听器接收到哪些代码?最后,什么时候执行showLabel()方法?我的猜测是,由于API是异步的(同步API不需要监听器),因此您在调用监听器之前执行该方法,但由于监听器是在某个点执行的,因此您将tts成员和正确的日志都视为null。尝试在onInit(又名侦听器)@TedEdwin的成功块内执行showLabel。请在函数中打印消息,并在运行时检查日志。在哪个序列消息中printed@FcoP. onInit()
内的tts正在工作。只是内部的showLabel为空是否有可能在创建之前执行OnCreate
?@TejasPandya我不确定。有没有办法看它是否先执行?有可能延迟超控功能吗?您好!你能提供航海日志吗?另外,您从侦听器接收到哪些代码?最后,什么时候执行showLabel()方法?我的猜测是,由于API是异步的(同步API不需要监听器),因此您在调用监听器之前执行该方法,但由于监听器是在某个点执行的,因此您将tts成员和正确的日志都视为null。尝试在onInit(又名侦听器)@TedEdwin的成功块内执行showLabel。请在函数中打印消息,并在运行时检查日志。在哪个序列消息中printed@FcoP. onInit()
内的tts正在工作。仅内部的showLabel为空
class ImageLabelingProcessor : VisionProcessorBase<List<FirebaseVisionImageLabel>>() {
private val detector: FirebaseVisionImageLabeler = FirebaseVision.getInstance().onDeviceImageLabeler
override fun stop() {
try {
detector.close()
} catch (e: IOException) {
Log.e(TAG, "Exception thrown while trying to close Text Detector: $e")
}
}
override fun detectInImage(image: FirebaseVisionImage): Task<List<FirebaseVisionImageLabel>> {
return detector.processImage(image)
}
override fun onSuccess(
originalCameraImage: Bitmap?,
labels: List<FirebaseVisionImageLabel>,
frameMetadata: FrameMetadata,
graphicOverlay: GraphicOverlay
) {
graphicOverlay.clear()
originalCameraImage.let { image ->
val imageGraphic = CameraImageGraphic(graphicOverlay, image)
graphicOverlay.add(imageGraphic)
}
val labelGraphic = LabelGraphic(graphicOverlay, labels)
graphicOverlay.add(labelGraphic)
graphicOverlay.postInvalidate()
val livePreview = LivePreviewActivity()
livePreview.showLabel(labels)
livePreview.labels = labels
}
override fun onFailure(e: Exception) {
Log.w(TAG, "Label detection failed.$e")
}
companion object {
private const val TAG = "ImageLabelingProcessor"
}
}
2019-03-09 14:54:50.979 9658-9658/com.google.firebase.samples.apps.mlkit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.firebase.samples.apps.mlkit, PID: 9658
kotlin.KotlinNullPointerException
at com.google.firebase.samples.apps.mlkit.kotlin.LivePreviewActivity.showLabel(LivePreviewActivity.kt:76)
at com.google.firebase.samples.apps.mlkit.kotlin.imagelabeling.ImageLabelingProcessor.onSuccess(ImageLabelingProcessor.kt:51)
at com.google.firebase.samples.apps.mlkit.kotlin.imagelabeling.ImageLabelingProcessor.onSuccess(ImageLabelingProcessor.kt:19)
at com.google.firebase.samples.apps.mlkit.kotlin.VisionProcessorBase$detectInVisionImage$1.onSuccess(VisionProcessorBase.kt:98)
at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-03-09 14:54:50.983 1881-1952/system_process W/ActivityManager: Force finishing activity com.google.firebase.samples.apps.mlkit/.kotlin.LivePreviewActivity
2019-03-09 14:54:50.997 9658-9658/com.google.firebase.samples.apps.mlkit I/Process: Sending signal. PID: 9658 SIG: 9
2019-03-09 14:54:51.004 1596-1596/? E/lowmemorykiller: Error writing /proc/9658/oom_score_adj; errno=22
2019-03-09 14:54:51.026 1712-9704/? E/Camera3-OutputStream: getBufferLockedCommon: Stream 0: Can't dequeue next output buffer: Broken pipe (-32)
2019-03-09 14:54:51.026 1712-9704/? E/Camera3-Device: RequestThread: Can't get output buffer, skipping request: Broken pipe (-32)