android - 当我将文本转语音 Android 应用程序放入选项卡时,它停止工作

标签 android tabs text-to-speech

我有一个文本转语音应用程序,它运行良好。事情是我必须把它放在一个选项卡中,并且选项卡工作正常。但是当我将 tts 放入选项卡中时,它就不再起作用了。我已经更改了类名、 View 布局和包,当然除了变量之外,其他所有内容都无法更改。布局不错啊不幸的是我真的必须把它放在一个选项卡中。但最糟糕的是它没有显示任何错误。即使 try catch 也捕获不到任何东西。所以我认为这是一种逻辑错误,它吸走了我头脑中的所有逻辑。哈哈。我检查了主要 Activity ,该 Activity 在其中被调用,并且很明显,它们是干净的。所以,这是代码:

package leytocz.add.andriod;

import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class nptab extends Activity implements TextToSpeech.OnInitListener{

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nptab);
        tts=new TextToSpeech(this,this);
        btnSpeak=(Button) findViewById(R.id.btnSpeak);
        txtText=(EditText) findViewById(R.id.txtText);

        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                speakOut();
            }
        });
    }
@Override
public void onInit(int status) {
    if (status==TextToSpeech.SUCCESS) {
        int result=tts.setLanguage(Locale.US);
        if (result==TextToSpeech.LANG_MISSING_DATA
                || result==TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS","This Language is not supported");
        } else {
            btnSpeak.setEnabled(true);
            speakOut();
        }
    } else {
        Log.e("TTS", "Initialization Failed!");
    }
}
private void speakOut() {
    String text=txtText.getText().toString();
    tts.speak(text,TextToSpeech.QUEUE_FLUSH, null);
}
}

最佳答案

正确。

static TextToSpeech mTTS;

onCreate():

Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

Activity 结果:

    if (requestCode == MY_DATA_CHECK_CODE && !mTTSInitialized) 
    {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) 
        {
            // success, create the TTS instance
            mTTS = new TextToSpeech(this, (OnInitListener) this);
            if (mTTS!=null)
                mTTSInitialized = true;
        } 
        else 
        {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }

setter/getter :

public static TextToSpeech getmTTS() {
    return mTTS;
}

所有这些都放置在 TabHost 本身中。

关于android - 当我将文本转语音 Android 应用程序放入选项卡时,它停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11806143/

相关文章:

android - Bottom Sheet 全屏显示

android - 如何在 Android 中进行图像处理?

javascript - 当 child 悬停并单击时设置父级样式

java - 如何在我的 TabPane 中的选项卡之间添加空格?

java - Processing/java 中文本到语音的长音节

安卓磨损 : Programmatically stop my app's notifications from appearing on watch

android - onCreate() 没有被调用

android - pager.setAdapter() 中的空指针异常

c# - C#Google Cloud文字转语音-Wavenet问题

java - 某些语言环境在 TTS 中不可用——包括西类牙语