Java 在CustomListview中使用TTS时出现NullPointerException

Java 在CustomListview中使用TTS时出现NullPointerException,java,android,text-to-speech,Java,Android,Text To Speech,我有自定义的listview,它包含内容和TTS选项,当使用TTS时,它抛出空指针异常,listview也不会出现,它显示我的应用程序已停止。错误显示在Logcat中 Applicationadapter.java public class ApplicationAdapter extends ArrayAdapter<Application> implements TextToSpeech.OnInitListener{ private List<Applicatio

我有自定义的listview,它包含内容和TTS选项,当使用TTS时,它抛出空指针异常,listview也不会出现,它显示我的应用程序已停止。错误显示在Logcat中

Applicationadapter.java

public class ApplicationAdapter extends ArrayAdapter<Application> implements
TextToSpeech.OnInitListener{
    private List<Application> items;
    private LayoutInflater inflator;
    private MainActivity activity;

    private ProgressDialog dialog;
    public TextToSpeech tts;
    public ImageButton btnaudioprayer;
   public TextView text1;

    ArrayAdapter<String> adapter;

    public ApplicationAdapter(MainActivity context, List<Application> items){
        super(context, R.layout.activity_row, items);
        this.items = items;

        inflator = LayoutInflater.from(getContext());
        activity=context;


    }

    @Override
    public int getCount(){
        return items.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;

        tts = new TextToSpeech(activity, ApplicationAdapter.this);


        //View v = convertView;
        if ( convertView == null ){ 
            convertView = inflator.inflate(R.layout.activity_row, null);
            holder = new ViewHolder();
            holder.text2 = (TextView) convertView.findViewById(R.id.text2);
            holder.text1 = (TextView) convertView.findViewById(R.id.text1);
            holder.count = (TextView) convertView.findViewById(R.id.count); 
            holder.pray  = (Button) convertView.findViewById(R.id.pray);
            holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
            holder.btnSpeak = (ImageButton) convertView.findViewById(R.id.btnaudioprayer);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view,
                    boolean isChecked) {
                int getPosition = (Integer) view.getTag();
                items.get(getPosition).setSelected(view.isChecked());

            }
        });

        holder.pray.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int getPosition= (Integer)v.getTag();
                StringBuffer sb1 = new StringBuffer();
                sb1.append("ID :");
                sb1.append(Html.fromHtml(""+items.get(getPosition).getId()));
                sb1.append("\n");
                activity.praydata(items.get(getPosition).getId());

            }


        });


         holder.btnSpeak.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View V) {


                   speakOut();
                }

            }); 

        Application app = items.get(position);
        holder.chk.setTag(position);
        holder.pray.setTag(position);
        holder.text2.setText(Html.fromHtml(app.getTitle()));
        holder.text1.setText(Html.fromHtml(app.getContent()));
        holder.count.setText(app.getCount()+"");
        holder.chk.setChecked(app.isSelected());

        return convertView;
    }
    static class ViewHolder {
        public TextView text2;
        public TextView text1;
        public TextView count;
        public CheckBox chk;
        public Button pray;
        public ImageButton btnSpeak;
        private TextToSpeech tts;
    }
    @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 {

                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }


    }

    private void speakOut() {

            String text = text1.getText().toString();

            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }

在我的custome listview中,我有文本和TTS按钮,当单击按钮时,它必须播放内容的音频。

您的变量
text1
未初始化,但已声明,因此执行
text1.getText()
,您实际上是在尝试从
null
执行
getText()


也许你想要的是
holder.text1
ViewHolder
中的
holder
)?

我会删除
speakOut()的调用
onInit(int status)
中,我将修改
speakOut()功能如下:

private void speakOut(String text) {
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
你的onclicklistener我会在这里改成:

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

这应该对你有用。

他可能想要
getView
中的文本1
holder.text1
。是的,我想要text1,因为我想把text1转换成音频,text1在holder中,如何得到它one@BuhakeSindi,Raghunandan是正确的,我只想要文本1 DudelCare类成员在按钮上获取文本单击将其分配给变量,然后调用
speakOut()
@Raghunandan什么dude,我必须做的do@Raghunandan我只想把text1转换成d@Raghunandan你在吗,它再次抛出空指针异常后,改变了布局,现在改变了我的布局后,它在那一行显示错误TextView tv=vh.text1;它再次抛出空指针异常
 @Override
 public void onClick(View v) {
     View parent = (View)v.getParent();
     ViewHolder vh = (ViewHolder)parent.getTag();
     TextView tv = vh.text1;
     speakOut(tv.getText().toString());
 }