Listview设置适配器片段nullpointerexception

时间:2014-02-15 16:26:55

标签: android listview nullpointerexception fragment adapter

我目前有3个片段,但我无法让我的适配器正常工作。 当我调用lv.setAdapter(lbadapter)时,它返回一个nullpointer异常。 可能是什么问题?

提前致谢

这是我片段的一部分:

@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);


    }

}

这是我的leadlist.xml文件:

<?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>

我的适配器:

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





}

3 个答案:

答案 0 :(得分:8)

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

而是这样做。由于列表视图与您的Activity没有直接关系。它是片段内的封闭视图,其中包含ListView

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

答案 1 :(得分:1)

可能你需要这样做

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

而不是

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

假设列表视图位于片段中。

答案 2 :(得分:0)

问题在于初始化ListView的方式。如果您发现它属于您的leadlist.xml文件。但是,在初始化时,您正在呼叫,

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

将此更改为,

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

它应该有效。告诉我它是怎么回事。快乐的编码:)