如何在没有视图也没有标题栏的应用程序中使用 TextToSpeech

问题描述

我正在尝试编写一个应用程序,该应用程序使用 TextToSpeech 说出一条简短的消息,而不会干扰屏幕上显示内容

MainActivity.kt 内容如下:

package com.example.nearbyplaces

import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.util.*

class MainActivity : AppCompatActivity(),TextToSpeech.OnInitListener {

    private var mTTS: TextToSpeech? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mTTS = TextToSpeech(this,this)
    }

    override fun onInit(status: Int) {
        if (status == TextToSpeech.SUCCESS) {
            val result = mTTS!!.setLanguage(Locale.ENGLISH)
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS","The Language specified is not supported!")
            } else {
                speak("Nearby places")
            }
        } else {
            Log.e("TTS","Initilization Failed!")
        }
    }

    private fun speak(mText: String) {
        mTTS!!.speak(mText,TextToSpeech.QUEUE_FLUSH,null,"")
    }

    override fun onDestroy() {
        if (mTTS != null) {
            mTTS!!.stop()
            mTTS!!.shutdown()
        }
        super.onDestroy()
    }
}

AndroidManifest.xml 是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nearbyplaces">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

使用认的空白布局,应用程序可以工作,但在之前的视图之上显示认的 Hello World 视图(带有标题栏)。如果删除 setContentView(R.layout.activity_main) 行并安全删除 activity_main.xml,仍会显示带有标题栏的空白视图。

遵循有关 separate thread 的建议,我尝试在清单中插入 android:theme="@android:style/Theme.Translucent.NoTitleBar"。这与一个显示 Toast 的应用程序一起工作正常,但在带有 TextToSpeech 代码的应用程序中,我收到错误“您需要在此活动中使用 Theme.AppCompat 主题(或后代)。”。 LogCat 报告:

2021-02-28 16:37:40.378 17048-17048/? I/le.nearbyplace: Late-enabling -Xcheck:jni
2021-02-28 16:37:40.411 17048-17048/? E/le.nearbyplace: UnkNown bits set in runtime_flags: 0x8000
2021-02-28 16:37:41.240 17048-17048/com.example.nearbyplaces I/TextToSpeech: Sucessfully bound to com.google.android.tts
2021-02-28 16:37:41.304 17048-17048/com.example.nearbyplaces D/AndroidRuntime: Shutting down VM
2021-02-28 16:37:41.306 17048-17048/com.example.nearbyplaces E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.nearbyplaces,PID: 17048
    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:843)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
        at androidx.appcompat.app.AppCompatDelegateImpl.onPostCreate(AppCompatDelegateImpl.java:527)
        at androidx.appcompat.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:127)
        at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1402)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3528)
        at android.app.servertransaction.TransactionExecutor.performlifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycletoPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2183)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:241)
        at android.app.ActivityThread.main(ActivityThread.java:7604)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
2021-02-28 16:37:41.358 17048-17048/com.example.nearbyplaces I/Process: Sending signal. PID: 17048 SIG: 9

是否有带有 NoTitleBar 的“Theme.AppCompat 主题(或后代)”,或者有任何其他方法可以防止出现带有标题栏的空白视图?

解决方法

您可以以编程方式隐藏和显示操作栏:

 actionBar?.hide()
 actionBar?.show()

或者选择一个没有操作栏的主题:

 <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

对于 Hello World 视图,只需将其从 activity_main.xml 中删除。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...