Search code examples
javaandroidandroid-layouttext-to-speech

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.

Here my 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.

Error line is TextView tv = vh.text1;


Solution

  • 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; being null

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

    Your vh.text1 is not initialized. You will get NPE.

    So just set the button tag with the value and get the tag in button click and cal speakout.