【问题标题】:Listview set adapter fragment nullpointerexceptionListview设置适配器片段nullpointerexception
【发布时间】:2014-03-15 01:11:52
【问题描述】:

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

提前致谢

这是我的片段的一部分:

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





}

【问题讨论】:

标签: android listview nullpointerexception fragment adapter


【解决方案1】:
lv = (ListView) this.getActivity().findViewById(android.R.id.list);

改为这样做。由于列表视图与您的Activity 没有直接关系。它是片段内的封闭视图,它持有ListView

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

【讨论】:

  • 谢谢它的工作。顺便问一下其他问题,你需要为每个片段单独布局还是一个布局就足够了?
  • 这取决于您需要在片段中显示的内容。
  • 如果您需要在每个片段上使用相同的 UI,则可以正常工作。
  • 是的,在这种情况下,您可以使用相同的片段本身
【解决方案2】:

也许你需要这样做

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

而不是

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

假设列表视图在片段中。

【讨论】:

  • 如果您查看时间差异,您可以缩小范围。
【解决方案3】:

问题在于您初始化 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);

它应该可以工作。让我看看情况如何。快乐编码:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-19
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多