动态添加列表视图

我在Android的development.i新希望add listview dynamically 。我是能够添加静态数组但是当我做了动态数组我得到错误,请帮助me.catName是diplay完善的日志。 main.xml中我加入linerlayout 1的ListView这是我的代码 -

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 <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<String>(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(); }

我的日志CAT-

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)

感谢致敬

Arpit特里维迪

--------------解决方案-------------

卸下for()循环这一行

catNames=new String[sitesList.getCat_Name().size()];

initialize it before for() loop

并让我知道发生什么事。

分类:安卓 时间:2015-03-15 人气:0
本文关键词: 安卓
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.141 (s). 10 q(s)