如何指定要在List <parseobject>中添加的parseObject

时间:2016-01-25 18:29:14

标签: android list parse-platform

我试图根据当前用户的用户名在我的列表中添加一些特定的ParseObject。但是这段代码只在行上的NullPointerException中结束:

query.findInBackground(new FindCallback()

mStatus.add(status.get(I));

有人请帮我解决这个问题。谢谢。

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = null;
    view = inflater.inflate(R.layout.update_status, container, false);

    ParseUser currentUser = ParseUser.getCurrentUser();
    final String strCurrentUsername = currentUser.getUsername();
    if (currentUser != null) {

        //try
        final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Status");
        query.orderByDescending("createdAt");

        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(final List<ParseObject> status, ParseException e) {
                if(e == null){
                    //success
                    for(int i = 0; i <status.size();i++){
                        String usernow = status.get(i).getString("user");
                        if(usernow.equals(strCurrentUsername)){
                            Toast.makeText(getActivity(), "num"+i, Toast.LENGTH_SHORT).show();
                            mStatus.add(status.get(i));
                        }
                        else i++;
                    }
                    //mStatus = status;
                    StatusAdapter statusAdapter = new StatusAdapter( getListView().getContext(), mStatus);
                    setListAdapter(statusAdapter);

                }else{
                    //there was a problem

                }
            }
        });


    } else {
        Intent intent = new Intent(getActivity(), FirstActivity.class);
        startActivity(intent);
    }

    return view;
}

1 个答案:

答案 0 :(得分:0)

mStatus是否已初具资格? mStatus是一个类字段吗?我不知道你在哪里获得null异常,但我测试了你的代码,这对我来说很有用,其中user是我的解析数据库中的一列,它的值是一个字符串:

 ParseUser currentUser = ParseUser.getCurrentUser();
    final String strCurrentUsername = currentUser.getUsername();
    if (currentUser != null) {

        //try
        final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Status");
        query.orderByDescending("createdAt");

        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(final List<ParseObject> status, ParseException e) {
                if (e == null) {
                    ArrayList<ParseObject> mStatus = new ArrayList<ParseObject>();
                    //success
                    for (int i = 0; i < status.size(); i++) {
                        String usernow = status.get(i).getString("user");
                        if (usernow.equals(strCurrentUsername)) {

                            mStatus.add(status.get(i));
                        } else i++;
                    }


                } else {
                    //there was a problem

                }
            }
        });


    } else {

    }