当我把我的文本到语音android应用程序放在选项卡中时,它停止了工作



我有一个文本到语音应用程序,它运行得很好。问题是我必须把它放在一个标签中,标签工作得很好。但当我把tts放在标签里时,它就不起作用了。我已经将类名、布局更改为视图和包,当然,除了变量之外,其他一切都是不可更改的。布局很好。不幸的是,我真的不得不把它放在一个标签里。但最糟糕的是,它没有显示任何错误。即使尝试抓也抓不到任何东西。所以我认为这是一种逻辑错误,它吸取了我头脑中的所有逻辑。哈哈。我检查了主活动,这个活动被调用的地方,并且清单,它们是干净的。所以,这是代码:

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);

onActivityResult:

    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);
        }
    }

Getter:

public static TextToSpeech getmTTS() {
    return mTTS;
}

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

不需要在你的活动中写那么多代码,只需单独写这一行,我也面临着这个问题,最终我得到了这个解决方案。

更改此

tts=new TextToSpeech(this,this); --this is your code

你像这样改变

tts=new TextToSpeech(getParent(),this);

它将非常适合我

最新更新