好吧..。我正在尝试使用TTS引擎创建一个应用程序。
我已经能做到了,工作,没问题。然而,我需要我的按钮是迪纳美,他们将来自一个数据库。
到目前为止,你们帮助了我很多,从现在起,我可以做到这一点,多亏了我从你们那里得到的提示。。
井..。现在我又被困住了。
我创建的每个新按钮都会附加一个OnClickListener,这样它就可以启动TTS并说点什么了。
但是,它是一个内部方法,因此,当我尝试运行下面的代码时,当它试图使用TTS“说话”时,它会给我一个NullPointerException。我知道TTS对象在上下文之外,所以,我如何解决这个问题?
在密码下面。它有点大,因为我想把所有的东西都包括进去:
请跳到“这是我的问题!”好让你们知道我的问题出在哪里。我知道它在哪里,但我不知道如何解决它=
任何帮助都很感激!=)
public class LivoxTesteActivity extends Activity implements OnInitListener{
/** Called when the activity is first created. */
private int MY_DATA_CHECK_CODE = 0;
public TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
LinearLayout lgeral = new LinearLayout (this);
lgeral.setOrientation(LinearLayout.VERTICAL);
lgeral.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
String array[][] = {{"Comer","eat", "Eu quero comer", "1"},
{"Abraço","hug", "Eu quero um abraço", "2"},
{"Assustado","afraid", "Eu estou com medo", "3"},
{"Beber","drink", "Eu quero beber", "4"}};
int x = array.length;
int qtdeLinhas = 2;
for (int j = 0; j < qtdeLinhas; j++) {
LinearLayout l1 = new LinearLayout (this);
l1.setOrientation(LinearLayout.HORIZONTAL);
l1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
FrameLayout fl;
for (int i = 0; i < array.length; i++) {
fl = (FrameLayout)LayoutInflater.from(getBaseContext()).inflate(R.layout.framelayoutstyle, l1, false);
TextView textoEscrito;
textoEscrito = (TextView)LayoutInflater.from(getBaseContext()).inflate(R.layout.textviewstyle, fl, false);
textoEscrito.setText(array[i][0]);
final String texto = textoEscrito.getText().toString();
final String textoFalar = array[i][2];
ImageButton btn;
btn = (ImageButton)LayoutInflater.from(getBaseContext()).inflate(R.layout.imagebuttonstyle, fl, false);
btn.setImageResource(this.getResources().getIdentifier("drawable/" + array[i][1], null, this.getPackageName()));
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick (View v){
Toast.makeText(getBaseContext(), texto, Toast.LENGTH_SHORT).show();
//*******************************
//HERE IS MY PROBLEM!!!
//*******************************
tts.speak(txtFl, TextToSpeech.QUEUE_ADD, null);
//*******************************
//WHEN I TRY TO RUN THE ABOVE IT GIVES A NULLPOINTEREXCEPTION!!!
//*******************************
}
});
fl.addView(btn);
fl.addView(textoEscrito);
l1.addView(fl);
}
lgeral.addView(l1);
}
setContentView(lgeral);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
tts = new TextToSpeech(this, this);
}
else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
}
else if (status == TextToSpeech.ERROR) {
}
}
@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}顺便说一下..。方法Toast.makeText(getBaseContext(),texto,Toast.LENGTH_SHORT).show();工作正常。我相信那是因为烤面包是静态的。
那么,也许解决方案是用这个方法来创建一个静态类呢?想法?我该怎么做呢?
发布于 2011-10-30 18:44:00
在合成语音之前,一定要同时调用onActivityResult和onInit。如果您得到的是NullPointerException,最可能的原因是在单击按钮时,还没有构造tts对象。
正确的方法是建立一个短语队列来说话(比如一个ArrayList<String>),一旦TTS被初始化(即在onInit的成功分支中),就开始说话。
编辑:现在我看了你的代码。您在哪里调用将导致onActivityResult调用的任何活动?开发指南建议将以下操作作为init过程的一部分:
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);贴好的片段里什么也没有。
发布于 2011-10-30 18:21:49
声明tts为最终,它将成为可访问的。或者,您可以使它成为类的实例变量。你使用它的方式,后者可能是正确的答案。
https://stackoverflow.com/questions/7946742
复制相似问题