简体   繁体   English

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

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

I am using in TTS in my listview,while using TTS in Relative layout it works fine.Now i changed my layout to LinearLayout.Its throwing Null pointer exception for that txt,so while clicking the TTS button its showing My activity has stopped. 我在列表视图中使用TTS,而在相对布局中使用TTS则工作正常。现在我将布局更改为LinearLayout。它为该txt抛出Null指针异常,因此在单击TTS按钮时其显示我的活动已停止。

Here my Applicationadapter.java 这是我的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;
    }

its throwing Null pointer exception for the textview conversion part,but that textview is visible ,while clicking the button alone my application has stopped. 它为textview转换部分抛出的Null指针异常,但该textview是可见的,仅单击该按钮,我的应用程序已停止。

Error line is TextView tv = vh.text1; 错误行是TextView tv = vh.text1;

Try the below 试试下面

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

Then 然后

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
    }

};

From the comment 从评论

Your changing layout has nothing to do with TextView tv = vh.text1; 您不断变化的布局与TextView tv = vh.text1; being null 为空

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

Your vh.text1 is not initialized. 您的vh.text1未初始化。 You will get NPE. 您将获得NPE。

So just set the button tag with the value and get the tag in button click and cal speakout. 因此,只需将按钮标签设置为相应的值,然后在按钮单击和发出语音提示中获得标签即可。

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

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