dynamically adding listview
<p>I am new in android development.i want to <code>add listview dynamically</code>.i am able to add static array but when i did dynamically array i get error please help me.catName is diplay perfect in log.
In main.xml i add one listview in linerlayout
This is my code-</p>

<pre><code>public void onCreate(Bundle savedInstanceState) 
 {
    super.onCreate(savedInstanceState);

    System.setProperty("http.proxyHost","192.168.0.2");
    System.setProperty("http.proxyPort","8080");

    TextView Cat_id[];
    TextView Cat_Parentid[];
    TextView Cat_Name[];
    TextView Cat_Desc[];
    String str[]=new String[50];
    String[] temp=new String[50];
    TextView attribute[];
    ArrayAdapter adapter;
    String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
    try 
    {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        URL sourceUrl = new URL("http://mobileecommerce.site247365.com/admin/catdata.xml");

        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

    }catch(Exception e)
    {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    sitesList = MyXMLHandler.sitesList;

    Cat_id = new TextView[sitesList.getCat_id().size()];
    Cat_Parentid = new TextView[sitesList.getCat_id().size()];
    Cat_Name = new TextView[sitesList.getCat_id().size()];
    Cat_Desc=new TextView[sitesList.getCat_id().size()];
    String mystr,newstr;

    for (int i = 0; i &lt;sitesList.getCat_Name().size(); i++) 
    {
        catNames=new String[sitesList.getCat_Name().size()];

        Cat_Name[i] = new TextView(this);
        Cat_Name[i].setText("Cat_Name = "+sitesList.getCat_Name().get(i));
        catNames[i]=sitesList.getCat_Name().get(i);
        Log.v("catnames",catNames[i]);
        str[i]=sitesList.getCat_Desc().get(i);
            temp[i]=str[i];

        }

    setListAdapter(new ArrayAdapter&lt;String&gt;(this,
            android.R.layout.simple_list_item_1,catNames));
    getListView().setTextFilterEnabled(true);

    (this,android.R.layout.simple_list_item_1,catNames));

}
static final String[] PENS = new String[]{
    "MONT Blanc",
    "Gucci",
    "Parker",
    "Sailor",
    "Porsche Design",
    "Rotring",
    "Sheaffer",
    "Waterman"
   };
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Object o = this.getListAdapter().getItem(position);
    String pen = o.toString();
    Toast.makeText(this, "You have chosen ::" + " " + pen, Toast.LENGTH_LONG).show();
   }
</code></pre>

<p>My log cat-</p>

<pre><code>09-19 23:58:47.480: ERROR/AndroidRuntime(438): FATAL EXCEPTION: main
09-19 23:58:47.480: ERROR/AndroidRuntime(438): java.lang.NullPointerException
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.ListView.fillDown(ListView.java:652)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.ListView.fillFromTop(ListView.java:709)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.ListView.layoutChildren(ListView.java:1580)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.view.View.layout(View.java:7035)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.view.View.layout(View.java:7035)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.view.View.layout(View.java:7035)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.view.View.layout(View.java:7035)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.os.Looper.loop(Looper.java:123)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at java.lang.reflect.Method.invoke(Method.java:521)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-19 23:58:47.480: ERROR/AndroidRuntime(438):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>Thanks and Regards</p>

<p>Arpit Trivedi </p>
