0

listview.butにcatnamesを追加したいのですが、実行時にエラーが表示されます。これは私のコードでlogcat.iは静的配列を追加できますが、動的に使用するとエラーが表示されます。ListView動的に追加する項目

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.main); 


    TextView attribute[]; 
    ArrayAdapter adapter; 
    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); 


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

マイLOGCAT--

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

答えて

1

ArrayAdapterあるアダプタは、初期化されていないため、NullPointerExceptionがあります。そのコンストラクタを呼び出し、正しく初期化してください。この問題は歴史になります。この(存在しない)オブジェクトのメソッドを呼び出そうとすると、ランタイムエラーが発生します。 adapterに値を代入すると、このオブジェクトに対してNullPointerExceptionが再度発生することはありません。他のバグもあるかもしれませんが、私たちの問題は、初期化されていないオブジェクト(初期化なしのヌルポインタ)のメソッドを呼び出すとスローされるNullPointerExceptionです。

関連する問題