使用ListView和Fragments

时间:2017-02-11 19:26:56

标签: android listview nullpointerexception

我正在运行一个应用程序,我无法理解为什么我收到此错误消息,这里是堆栈跟踪

  

E / AndroidRuntime:致命异常:主要                     过程:com.kozmiksoftware.swaggster,PID:10428                     java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'                         在com.kozmiksoftware.swaggster.ListView.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:62)                         在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6279)                         在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6312)                         在android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5258)                         在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5521)                         在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5363)                         在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5359)                         在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2141)                         在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)                         在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)                         在android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1305)                         在android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1050)                         在android.support.v7.widget.RecyclerView $ ViewFlinger.run(RecyclerView.java:4649)                         在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:871)                         在android.view.Choreographer.doCallbacks(Choreographer.java:683)                         在android.view.Choreographer.doFrame(Choreographer.java:616)                         在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:857)                         在android.os.Handler.handleCallback(Handler.java:751)                         在android.os.Handler.dispatchMessage(Handler.java:95)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我有一个片段,我正在尝试向listview添加标题,我构建了一个自定义适配器,它列在下面

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;

Header header;
List<ListItem> listItems;

public RecyclerAdapter(Header header, List<ListItem> listItems)
{
    this.header = header;
    this.listItems = listItems;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if(viewType == TYPE_HEADER)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
        return new VHHeader(v);
    }
    else if(viewType == TYPE_ITEM)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);
        return new VHHeader(v);
    }

    throw new RuntimeException("There is no type that matches the type " + viewType + " + make sure your using types correctly");

}

private ListItem getItem(int position)
{
    return listItems.get(position);
}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if(holder instanceof VHHeader)
    {
        VHHeader VHheader = (VHHeader)holder;
        VHheader.txtFullname.setText(header.getFullname());
        VHheader.txtWeb.setText(header.getWeb());
        VHheader.txtBio.setText(header.getBio());
        VHheader.txtSwaggCount.setText(header.getSwaggCount());
        VHheader.txtFollowerCount.setText(header.getFollowerCount());
        VHheader.txtFollowingCount.setText(header.getFollowingCount());
    }
    else if(holder instanceof VHItem)
    {
        ListItem currentItem = getItem(position - 1);
        VHItem VHItem = (VHItem)holder;
        VHItem.txtName.setText(currentItem.getName());
        VHItem.iv.setImageResource(currentItem.getId());
    }

}

// Need to override this method
public int getItemViewType(int position) {
    if(isPositionHeader(position))
        return TYPE_HEADER;
    return TYPE_ITEM;
}

private boolean isPositionHeader(int position){
    return position == 0;
}

@Override
public int getItemCount() {
    return listItems.size()+1;
}

private class VHHeader extends RecyclerView.ViewHolder {
    TextView txtFullname;
    TextView txtWeb;
    TextView txtBio;
    TextView txtSwaggCount;
    TextView txtFollowerCount;
    TextView txtFollowingCount;
    public VHHeader(View itemView) {
        super(itemView);
        this.txtFullname = (TextView)itemView.findViewById(R.id.fullnameTV);
        this.txtWeb = (TextView)itemView.findViewById(R.id.webTV);
        this.txtBio= (TextView)itemView.findViewById(R.id.bioTV);
        this.txtSwaggCount = (TextView)itemView.findViewById(R.id.swaggsCountTV);
        this.txtFollowerCount = (TextView)itemView.findViewById(R.id.followerCountTV);
        this.txtFollowingCount = (TextView)itemView.findViewById(R.id.followingCountTV);
    }
}

private class VHItem extends RecyclerView.ViewHolder {
    TextView txtName;
    ImageView iv;
    public VHItem(View itemView){
        super(itemView);
        this.txtName = (TextView)itemView.findViewById(R.id.txtName);
        this.iv = (ImageView)itemView.findViewById(R.id.ivListItem);
    }
}

}

这是片段类

public class Posts extends Fragment {
private RecyclerView mPostRecyclerView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.post, container, false);
    mPostRecyclerView = (RecyclerView) rootView.findViewById(R.id.postListView);
    mPostRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    RecyclerAdapter adapter = new RecyclerAdapter(getHeader(), getListItems());
    mPostRecyclerView.setAdapter(adapter);

    return rootView;
}


private Header getHeader()
{
    ParseUser currentUser = ParseUser.getCurrentUser();
    String fullname = (String) currentUser.get("fullname");
    String bio = (String) currentUser.get("bio");
    String web = (String) currentUser.get("web");

    Header header = new Header();
    header.setFullname(fullname);
    header.setBio(bio);
    header.setWeb(web);
    header.setFollowerCount("1000");
    header.setFollowingCount("1000");
    header.setSwaggCount("1000");

    return header;

}

public  List<ListItem> getListItems()
{
    List<ListItem> listItems = new ArrayList<ListItem>();
    for (int i = 0; i<10; i++) {
        ListItem item = new ListItem();
        item.setName("image" + i);
        if (i % 2 == 0)
            item.setId(R.drawable.sweetlife);
        else
            item.setId(R.drawable.young);
        listItems.add(item);
    }
    return listItems;
}

}

它说错误在onBindViewHolder()方法中,我放了一个断点并检查了适配器,似乎没有我找到的空对象,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

当你的物品是普通物品时,你必须返回VHItem。你在两种类型

中返回VHHeader
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int         viewType) {
if(viewType == TYPE_HEADER)
{
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
    return new VHHeader(v);
}
else if(viewType == TYPE_ITEM)
{
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);
    return new VHItem(v);
}

throw new RuntimeException("There is no type that matches the type " + viewType + " + make sure your using types correctly");

}