David
David

Reputation: 850

Listview set adapter fragment nullpointerexception

I currently have 3 fragments but I am not able to get my adapter working. When I am calling lv.setAdapter(lbadapter) it returns a nullpointer exception. What could be the problem ?

Thanks in advance

Here's a part of my fragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView;
    rootView = inflater.inflate(R.layout.leadlist, container, false);



    profielen = new ArrayList<Profile>();
    new getLeaderboards().execute(url);

    lv = (ListView) this.getActivity().findViewById(android.R.id.list);
    lbadapter = new LeaderBoardAdapter(this.getActivity(), R.layout.item_layout, profielen);


    return rootView;
}

public static class getLeaderboards extends AsyncTask<String, Void, JsonObject> {


    @Override
    protected JsonObject doInBackground(String... urls) {

        JsonObject x = parseURL.GetJSON(urls[0]);

        return x;
    }


    @Override
    protected void onPostExecute(JsonObject obj) {
        Gson gson = new Gson();
        JsonArray arr = obj.get("rows").getAsJsonArray();

        for (JsonElement el : arr) {
            Profile pat = gson.fromJson( el , Profile.class);
            profielen.add(pat);
        }

        lv.setAdapter(lbadapter);


    }

}

Here's my leadlist.xml file :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

 <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:divider="@null"
    android:dividerHeight="20dp" >
</ListView>


</RelativeLayout>

My adapter :

    public class LeaderBoardAdapter extends ArrayAdapter<Profile>{

LayoutInflater vi;
List<Profile> profielen;
Context mContext;
int res;
public LeaderBoardAdapter(Context context, int resource,
        List<Profile> objects) {
    super(context, resource, objects);
    this.res = resource;
    this.profielen = objects;
    mContext = context;
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub


    ProfileHolder holder = null;
    if(convertView == null)
    {
        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
        convertView = vi.inflate(R.layout.item_layout, parent, false);
        convertView = inflater.inflate(res, parent, false);

        holder = new ProfileHolder();
        holder.name = (TextView)convertView.findViewById(R.id.nameChar);
        holder.pic = (ImageView)convertView.findViewById(R.id.imageChar);
        holder.rank = (TextView)convertView.findViewById(R.id.ratingNumber);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ProfileHolder)convertView.getTag();
    }

    Profile p = profielen.get(position);
    holder.name.setText(p.getName());
    holder.rank.setText(p.getRanking());

    return convertView;

}

public static class ProfileHolder{
    TextView name;
    ImageView pic;
    TextView rank;
}





}

Upvotes: 3

Views: 10445

Answers (3)

Atul O Holic
Atul O Holic

Reputation: 6792

The problem lies in the way you have initialized your ListView. If you notice it belongs to your leadlist.xml file. However while initializing you are calling,

lv = (ListView) this.getActivity().findViewById(android.R.id.list); // this means your listView lv belongs to your MainActivity which actually doesn't.

Change this to,

lv = (ListView) rootView.findViewById(android.R.id.list);

and it should work. Lemme me how it goes. Happy coding :)

Upvotes: 0

umair.ali
umair.ali

Reputation: 2714

May be you need to do like that

lv = (ListView) rootView.findViewById(android.R.id.list);

instead of

lv = (ListView) this.getActivity().findViewById(android.R.id.list);

Assuming the list view is in the fragment.

Upvotes: 1

Triode
Triode

Reputation: 11357

lv = (ListView) this.getActivity().findViewById(android.R.id.list);

Instead do this. Since the list view is not directly related to your Activity. It is the enclosing view inside the fragment which is holding the ListView

lv = (ListView) rootView.findViewById(android.R.id.list);

Upvotes: 8

Related Questions