开发者

android listview nullpointer.exception

Im trying to display this listview, but I keep getting a:

07-17 21:14:22.233: ERROR/AndroidRuntime(349): java.lang.NullPointerException

I think I know what the problem is but I dont know how to fix it.

I have a String array which I will be using to be displayed in my list

parser topicparser = new parser();
  final String[] TOPICS = topicparser.topic("http://www.t开发者_开发百科est.com");

Dont worry the parser works great. the only problem I see with it is that the parser doesnt know how many strings the website will have so I am setting the String array in the parser to be very big:

String substr[] = new String[250];

but I know that mostly there are only like 11 to 13 values that I will be storing in that array, the problem I think is when i do this:

setListAdapter(new ArrayAdapter<String>(this, R.layout.topics, TOPICS));

  ListView lv = getListView();

since the String array is set to have up to 250 values in it and I only actually store say 12 then there are a null entries so when I run the code it shows me my list view, but when I get to the bottom of the view it force closes and the log tells me that there was nullpointer. I can tell how many entries I collected while I am parsing, but how do I tell my listview to only show say 12 items in the list so there wont be any null entries. or is there any other solution for my problem.

Thank you,

@Cristian C.

I tried doing what you said, but now I am getting a:

java.lang.ClassCastException: java.util.Arrays$ArrayList

this is what I did:

String[] TOPICS = topicparser.topic("test.com"); 
ArrayList<String> niceArray = (ArrayList<String>) Arrays.asList(TOPICS); // eclipse told me I had to add the cast 
setListAdapter(new ArrayAdapter<String>(this, R.layout.topics, niceArray)); 

any ideas ? Thanks


Instead of using a normal array, use a ArrayList.

To convert the normal array to ArrayList, you can do something like:

ArrayList<String> niceArray = new ArrayList(Arrays.asList(normalArray));

Those classes are included in the java.util package.


instead of returning an array of strings in your topic method, I would return a List

so instead of

String substr[] = new String[250]

you could write

ArrayList<String> substr = new ArrayList<String>();

so now you can just use

substr.add("whatever string");

the arraylist will only be as big as the items that you add, so you won't have to worry about nullpointer exceptions

and since the topic method now returns an ArrayList, the last part can be rewritten as

niceArray = topicparser.topic("test.com");
setListAdapter(new ArrayAdapter<String>(this, R.layout.topics, niceArray));


I solved this problem by overriding getView method.


class MyArrayAdapter extends ArrayAdapter<String> {

    private int resource;

    public  MyArrayAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        this.resource = resource;
    }

    /**
     * get new element
     *  
     * @param position - add position
     * @param convertView - previous element
     * @param parent - ListView instance 
     * @see android.widget.ArrayAdapter#getView(int, android.view.View, 
     * android.view.ViewGroup) 
     */
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView v = (TextView) TextView.inflate(getContext(), this.resource, null);
        v.setText(this.getItem(position));
        return v;
    }  
}
:
setListAdapter(new MyArrayAdaptor(this, R.layout.topics, niceArray));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜