Android Parse.com使用自定义适配器问题填充listview

时间:2014-03-25 16:19:48

标签: java android listview nullpointerexception parse-platform

从parse.com中检索数据时,我收到NullPointerException。我已经跟踪了该程序,我确实收到了数据。我将在我的源代码中复制适配器和完成方法。

FATAL EXCEPTION: main java.lang.NullPointerEXception at adapter.GroupChatAdapter.add(GroupChatFragment.java.79)

这是我的自定义添加

    public class GroupChatAdapter extends ArrayAdapter<GroupChat>{
private List<GroupChat> chat;
private Group subscrib;
private ListView listView;

public GroupChatAdapter(Context context,
        int textViewResourceId) {
    super(context,textViewResourceId);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
    View v = convertView;
    if(v == null){
        LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_row_group_chat, null);
    }
    GroupChat cht = chat.get(position);
    if(cht != null){
        TextView userNameText =(TextView) v.findViewById(R.id.userName);
        TextView userPostText = (TextView) v.findViewById(R.id.userPost);
        if(userNameText != null){
            userNameText.setText(cht.userName);
        }
        if(userPostText!=null){
            userPostText.setText(cht.userComment);
        }
    }
    return v;
}
public void add(GroupChat chat){
    this.chat.add(chat);
}

    }

这是异常发生的地方

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_group_chat, container, false);
    mListView = (ListView) rootView.findViewById(android.R.id.list);

    return rootView;

}
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Bundle args = getArguments();
    if (args  != null && args.containsKey("groupName")){
        groupNamePassed = args.getString("groupName");
    }

    chatsList = new ArrayList<GroupChat>();
    mAdapter = new GroupChatAdapter(getActivity(),android.R.id.list);
    ParseQuery<ParseObject> query = ParseQuery.getQuery("comments");
    query.setLimit(1000);
    query.whereEqualTo("groupName",groupNamePassed);
    query.orderByAscending("createdAt");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            // TODO Auto-generated method stub
            for (ParseObject groups : objects) {
                chatchat = new GroupChat(groups.getString("user").toString(),groups.getString("groupComments").toString());
                mAdapter.add(chatchat);
            }
        }
    });

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onActivityCreated(savedInstanceState);
    mListView.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();
    messageToSend = (TextView)getView().findViewById(R.id.sendMessageText);
    Button submitPost = (Button) getView().findViewById(R.id.sendMessageButton);
    submitPost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle args = getArguments();
            if (args  != null && args.containsKey("groupName")){
                groupNamePassed = args.getString("groupName");
            }
            String message = messageToSend.getText().toString().trim();
            ParseObject commentParse = new ParseObject("comments");
            commentParse.put("groupName",groupNamePassed);
            ParseUser user = ParseUser.getCurrentUser();
            commentParse.put("user",user.get("username").toString());
            commentParse.put("groupComments",message);
            commentParse.saveInBackground();

            mAdapter.add(new GroupChat(user.get("username").toString(),message));
            mAdapter.notifyDataSetChanged();
        }
    });


}

mAdapter.add(chatchat)导致我的应用崩溃。我没看到我设置错误。任何帮助将不胜感激......

2 个答案:

答案 0 :(得分:0)

你在视图分配中做错了所以你必须遵循这个自定义适配器它会帮助你绝对最好....

public class CustomAdapter extends ArrayAdapter<Sample> {

public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;

public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
this.context = context;
inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getPosition(Sample item) {
return super.getPosition(item);
}

@Override
public Sample getItem(int position) {
return mlist.get(position);
}

@Override
public int getCount() {
return mlist.size();
}

@Override
public long getItemId(int position) {
return super.getItemId(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);//Replace your layout....
TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
text2.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // you just put your Logic here And use this custom adapter to
        // load your Data By using this particular custom adapter to
        // your listview
                        //Change your imageview here

    }
});
return convertView;
}

}

答案 1 :(得分:0)

您使用的是自己的“列表聊天”,而不是ArrayAdapter提供的数组。在这种情况下,您需要使用对象初始化聊天引用(Java不会自动执行)。如果不这样做,'chat'指向null / junk实例并且执行chat.add()将抛出NullPointerException。

在GroupChatAdapter构造函数中,添加以下行: chat = new List();

您拥有的另一个选项是使用ArrayAdapter提供的默认列表。在这种情况下,删除聊天对象定义和add(GroupChat)函数定义。适配器自动提供和添加功能,您可以使用与onCreate()相同的方式。