Android的Parse.com填充自定义适配器的ListView问题 [英] Android Parse.com populate listview with custom adapter issue

查看:85
本文介绍了Android的Parse.com填充自定义适配器的ListView问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从parse.com retreiving数据时,接收一个NullPointerException异常。我已经通过该计划跟踪,我确实接收数据。我将在我的源$ C ​​$ C复制适配器和done方法。

I am receiving a NullPointerException when retreiving data from parse.com. I have traced through the program and I am indeed receiving data. I will copy in my source code for the adapter and done method.

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

    }

这里是在异常发生

here is where the exception is happening

@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)引起我的应用程序崩溃。我不看我所设置不正确。任何帮助将是AP preciated ...

mAdapter.add(chatchat) is causing my app to crash. I dont see what I have set up incorrectly. Any help would be appreciated...

推荐答案

您使用的是自己列出聊天而不是由ArrayAdapter提供的数组。在这种情况下,你需要初始化一个对象聊天引用(Java将不会自动做)。如果不这样做,聊天指向空/垃圾的实例,做chat.add()将抛出一个NullPointerException异常。

You are using your own "List chat" instead of the array provided by ArrayAdapter. In this case, you need to initialise the chat reference with an object (Java won't do it automatically). Without doing this, 'chat' points to a null/junk instance and doing chat.add() will throw a NullPointerException.

在您GroupChatAdapter建筑工,加入这一行:
聊天=新名单();

In your GroupChatAdapter constructer, add this line: chat = new List ();

你有另一种选择是使用ArrayAdapter提供的默认列表。在这种情况下,删除你聊天对象的定义并且加载(群聊)函数的定义。该适配器提供并自动添加功能,您可以用同样的方法,你在的onCreate)做(。

The other option you have is to use the default list provided by ArrayAdapter. In this case, delete you chat object definition and the add(GroupChat) function definition. The adapter provides and add function automatically, which you can use the same way as you are doing in onCreate().

这篇关于Android的Parse.com填充自定义适配器的ListView问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆