title: dynamically add and remove views in listview specific row in android
body: <p>In my application I have created <code>Custom ListView</code>.So as my question <strong>I want to add and delete some views to a particular row</strong> of <code>ListView</code>.According to that I coded. But It giving me <em>Null Pointer Exception</em>.<br>There is button(say add) in a row and I am visibling it to specific position.By clicking on it I am adding a layout with textView and Button(say delete).As success I able to add new layout.<br><em>But It is not Deleting that added layout</em>(null pointer exp.).<br>
  follwing is adapter class :<br></p>
  
  <pre><code>public class TestAdapter extends ArrayAdapter&lt;String&gt; {
      Context con;
      ArrayList&lt;String&gt; listdata;
  
      public TestAdapter(Context context, int textViewResourceId,ArrayList&lt;String&gt; myitem) 
      {
          super(context, textViewResourceId, myitem);
          this.con = context;
          this.listdata = myitem;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) 
      {
          LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          final LinearLayout.LayoutParams linearparams=new LinearLayout.LayoutParams(
                  android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
  
          if (convertView == null) 
          {
              convertView = inflater.inflate(R.layout.fieldlist, null);
          }
          TextView data = (TextView) convertView.findViewById(R.id.VTItemTextView);
          Button add=(Button)convertView.findViewById(R.id.add);
  
          final EditText amount=(EditText)convertView.findViewById(R.id.amount);
          final LinearLayout llmain=(LinearLayout)convertView.findViewById(R.id.llmain);
  
          if(listdata.get(position).equalsIgnoreCase("lorem"))
          {
              add.setVisibility(View.VISIBLE);
  
              add.setOnClickListener(new OnClickListener() 
              {                   
                  @Override
                  public void onClick(View v) 
                  {                       
                      final LinearLayout llcontent=new LinearLayout(con);
                      llcontent.setLayoutParams(linearparams);
                      final TextView textView=new TextView(con);
                      textView.setLayoutParams(linearparams);
                      textView.setText(amount.getText().toString());
                      textView.setTag(position);
                      final Button delete=new Button(con);
                      delete.setLayoutParams(linearparams);
                      delete.setText("delete");                       
                      delete.setTag(position);
                      llcontent.addView(textView);
                      llcontent.addView(delete);
                      llmain.addView(llcontent);                      
                      notifyDataSetChanged();
                      delete.setOnClickListener(new OnClickListener() {
  
                          @Override
                          public void onClick(View v) {
                               Integer index = (Integer) v.getTag();
                          //here is Null Pointer Exception
                              llcontent.removeViewAt(index.intValue()); 
  
                                  Integer index1 = (Integer)textView.getTag();
                                  llcontent.removeViewAt(index1.intValue()); 
  
                              notifyDataSetChanged();
  
                          }
                      });
                  }
              });
          }
          else
              {
                  add.setVisibility(View.GONE);
                  amount.setVisibility(View.GONE);
              }
          data.setText(listdata.get(position));
          return convertView;
      }
  }
  </code></pre>
  
  <p><br>LogCat :<br></p>
  
  <pre><code>  FATAL EXCEPTION: main
  java.lang.NullPointerException
  at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3603)
  at android.view.ViewGroup.removeViewAt(ViewGroup.java:3567)
  at com.example.testapp.TestAdapter$1$1.onClick(TestAdapter.java:91)
  at android.view.View.performClick(View.java:4204)
  at android.view.View$PerformClick.run(View.java:17355)
  at android.os.Handler.handleCallback(Handler.java:725)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5041)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><br> for test I created a separate demo and It Works fine But if I use this logic in another app it not works. Why this...?Help me...!</p>