繁体   English   中英

在将我的相对布局更改为Linearlayout时,它在TTS的textview上的抛出Null point异常

[英]While changing my Relative layout to Linearlayout its throwing Null point exception on textview for TTS

我在列表视图中使用TTS,而在相对布局中使用TTS则工作正常。现在我将布局更改为LinearLayout。它为该txt抛出Null指针异常,因此在单击TTS按钮时其显示我的活动已停止。

这是我的Applicationadapter.java

@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.btnaudioprayer = (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());
                //activity.showAlertView(sb1.toString().trim());
                //activity.praydata(Integer.parseInt(sb1.toString().trim()));
            }


        });


         holder.btnaudioprayer.setOnClickListener(new View.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());
                }

            }); 




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

它为textview转换部分抛出的Null指针异常,但该textview是可见的,仅单击该按钮,我的应用程序已停止。

错误行是TextView tv = vh.text1;

试试下面

 holder.btnaudioprayer.setTag(holder.text1.getText()); //set the tag to teh button
 holder.btnaudioprayer.setOnClickListener(mClickListener);

然后

OnClickListener mClickListener=  new OnClickListener()
{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i(".........","hello"+v.getTag());
        String s =  v.getTag().toString(); // get the tag on button clikc
        speakOut(s); // then call speakOut with the string
    }

};

从评论

您不断变化的布局与TextView tv = vh.text1; 为空

 TextView tv = vh.text1; // this is null. assigning textview text1 to tv.

您的vh.text1未初始化。 您将获得NPE。

因此,只需将按钮标签设置为相应的值,然后在按钮单击和发出语音提示中获得标签即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM