2011-10-17 10 views
3

私はアンドロイド開発で新しいです.iはadd listview dynamicallyになります。静的配列を追加することはできますが、動的配列を作成するとエラーが発生します。 はmain.xmlでは私はこれが私のコード - あるlinerlayout に動的にリストビューを追加する

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) 

cat-

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

私のログを1つのリストビューを追加おかげで、よろしく

Arpit Trivediの

+0

この行をforループの最初に置き、forループから削除します。 catNames = new String [sitesList.getCat_Name()。size()]; – user370305

+0

こんにちはuser370305 4 reply.which行を削除して、ループの直前にcatNameを初期化してください。 – arpit

+0

catNames = new String [sitesList.getCat_Name()。size()];この行はforのループを削除し、for()の前に初期化します。何が起こるか教えてください。 – user370305

答えて

0

はこれを削除しますfor()ループからの行

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

およびinitialize it before for() loop

と私に何が起こるか教えてください。