android - Listview 设置适配器 fragment 空指针异常

标签 android listview nullpointerexception fragment adapter

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

提前致谢

这是我的 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);


    }

}

这是我的 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;
}





}

最佳答案

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

而是这样做。由于 ListView 与您的 Activity 没有直接关系。它是包含 ListView

的 fragment 内的封闭 View
lv = (ListView) rootView.findViewById(android.R.id.list);

关于android - Listview 设置适配器 fragment 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21800295/

相关文章:

java - 使 TreeMap 比较器容忍 null

java - 什么是NullPointerException,我该如何解决?

android - 将 volley 请求的结果使用到 AutocompleteAdapter 中

android - 当 View 模型中更新另一个 LiveData 时触发 LiveData 成员的更新

java - 从先前的 Activity 获取文本并将其添加到另一个 Activity ListView

java - 如何修复: Resources$NotFoundException while calling Toast

android - Decoder_setSearch 返回 -1 异常

Android:拦截 Intent 以我的 Activity 附加到开启者应用程序结束

android - 使用 RecyclerView 制作基于客户列表适配器 Android 的水平 ListView

android - 内存异常会导致 NullPointerException 吗?