Friday, 15 July 2011

Android listView adaptor error -


I define this string array:

  String [] mCountryList = new string [200 ];  

and fill the variable with JSON data, and when I use that variable with a ArrayAdapter :

  ArrayAdapter & LT; String & gt; MyAdaptor = New Array Adapter & lt; String & gt; (Manactivity.es, android.r.litt.system_list_item_1, mCountryList);  

I get this error:

java.lang.NullPointerException

I think that string array < Code> McountryList can be zero values ​​for example:

  mCountryList [0] = "x"; McountryList [1] = "x"; MCountryList [2] = Null;  

How can I resolve this?

I get the stacktrace:

2459-2459 / com.example.behzad. Behmytour E / Android Rentaim: Fatal Exception: The main process: com.example.behzad.behmytour, PID: 2459 JavaklangkNullPointerException Android. On Widget.ArrayAdapter.createViewFromResource (ArrayAdapter.java:394) android.widget.ArrayAdapter.getView android.widget.ListView.makeAndAddView (ListView.java:1790) android.widget on android.widget.ListView.fillDown (ListView.java on .AbsListView.obtainView (AbsListView.java:2263) (ArrayAdapter.java:362): 691) android.widget.ListView.fillFromTop (ListView.java:752) android.widget.ListView.layoutChildren (ListView.java:1630) In android.widget.AbsListView.onLayout (AbsListView.java2091) on Android at View.View.layout (View.java:14817) on android.view.ViewGroup.layout (ViewGroup.java:4631)

It seems that one of your items is null , as you might have thought, but your question was not easily understood. The solution is, make sure it is not.

You may want to use a list instead of an array, and then only populate as many items. You have 200 items Together there is a steady array of space, of course you will always have 200 items.

Then use it:

  list & lt; String & gt; MCountryList = New Arreshochi & lt; String & gt; (); Instead of  

  string [] mCountryList = new string [200];  

and then to populate it, something like this:

  mCountryList.add ("your string");  

No comments:

Post a Comment