NullPointerException while using TTS in CustomListview

僤鯓⒐⒋嵵緔 提交于 2019-12-05 15:02:54

I would remove the call of speakOut(); in onInit(int status) and I would modify the speakOut(); function like this:

private void speakOut(String text) {
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

And your onclicklistener I would change to this here:

 @Override
 public void onClick(View v) {
     View parent = (View)v.getParent();
     ViewHolder vh = (ViewHolder)parent.getTag();
     TextView tv = vh.text1;
     speakOut(tv.getText().toString());
 }

That should work for you.

Your variable text1 is not initialized, but declared, hence doing a text1.getText(), you're essentially trying to getText() from null.

Perhaps what you want is the holder.text1 (holder from the ViewHolder)?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!