如何从Parse Cloud中删除解析对象?

时间:2016-01-28 16:27:33

标签: android mobile parse-platform

我正在尝试删除从ListView中单击的解析对象。我要删除的对象是selectedPost。

错误: java.lang.NullPointerException:尝试调用虚方法&bolt;任务rang.afterflight.Post.deleteInBackground()'在空对象引用上

MyPostsFragment:

public class MyPostsFragment extends Fragment {

ListView lv;
ArrayAdapter adapter;
ArrayList<Post> postArrayList;
Post selectedPost;
DeleteCallback callback;


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

    lv = (ListView) rootView.findViewById(R.id.mypost_listview);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // clicked on item show post
            selectedPost = postArrayList.get(position);
            Bundle bundle = new Bundle();
            bundle.putParcelable("data", (Parcelable) selectedPost);
            FragmentManager fm = getActivity().getFragmentManager();
            Fragment fragment = new rang.afterflight.fragments.SelectedPostFragment();
            fragment.setArguments(bundle);
            fm.beginTransaction().replace(R.id.content_main, fragment).commit();
        }

    });

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            showDialog();
            return true;
        }
    });

    return rootView;
}




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ownPost();

    callback = new DeleteCallback() {
        @Override
        public void done(ParseException e) {
            selectedPost.saveInBackground();
        }
    };


    setHasOptionsMenu(true);


}

public void ownPost(){
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");

    ParseUser currentUser = ParseUser.getCurrentUser();
    String user = currentUser.getUsername();

    postArrayList = new ArrayList<Post>();

    query.whereEqualTo("username", user);

    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> postList, ParseException e) {
            if (e == null) {
                for (ParseObject object : postList) {
                    Post newPost = new Post();
                    newPost.setAirportParse((String) object.get("airport"));
                    newPost.setDateParse((String) object.get("date"));
                    newPost.setTimeParse((String) object.get("time"));
                    newPost.setPersonsParse((String) object.get("persons"));
                    newPost.setAddressParse((String) object.get("address"));
                    newPost.setFlightnrParse((String) object.get("flightnr"));
                    newPost.setUsername((String) object.get("username"));
                    newPost.setImageFile((ParseFile) object.get("profilepic"));
                    newPost.setContactParse((String) object.get("contact"));

                    postArrayList.add(newPost);
                }

                adapter = new ListViewAdapter(getActivity(), R.layout.item_cardview, postArrayList);
                lv.setAdapter(adapter);
            }
        }
    });
}

private void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater factory = LayoutInflater.from(getActivity());
    final View view = factory.inflate(R.layout.dialog_deletepost, null);

    builder.setView(view);
    builder.setCancelable(false);
    builder.setTitle("Are you sure you want to delete?");
    builder.setPositiveButton("GO BACK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });

    builder.setNeutralButton("DELETE POST", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            selectedPost.deleteInBackground();
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

}

1 个答案:

答案 0 :(得分:1)

似乎selectedPost为空。

长按单元格时,您应该以与正常单击时相同的方式获得selectedPost

  lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            selectedPost = postArrayList.get(position);
            showDialog();
            return true;
        }
    });