在Parse.com中查询指针类的objectId - Android

问题描述 投票:0回答:2

我正在尝试从Parse中的User类中检索objectId作为字符串,但我得到一个NullPointer。

Log.w(getClass().toString(), ParseUser.getCurrentUser().getParseObject("currentGroup").getObjectId());

java.lang.NullPointerException: println needs a message

我正在查询列“currentGroup”,它是一个指向名为“Group”的类的指针。我想要的只是检索它的objectId。 currentGroup和Group objectId列都包含数据。它们不是空的。这里发生了什么?

错误:

它似乎认为对象是null,但我不明白它为什么会这样。

java.lang.NullPointerException: Attempt to invoke virtual method
  'java.lang.String com.parse.ParseObject.getObjectId()' on a null object reference
at at com.yitter.android.fragment.FeedFragment.retrieveData(FeedFragment.java:112)
at com.yitter.android.fragment.FeedFragment.onCreateView(FeedFragment.java:105)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:18794)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18794)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1085)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18794)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
android parse-platform nullpointerexception
2个回答
1
投票
  ParseQuery<ParseObject> query = ParseQuery.getQuery("Orders");
        ParseObject obj = ParseObject.createWithoutData("ShopLocations",idd); // this pointer object class name and pointer value
        query.whereEqualTo("shop", obj); // this pointer object and parse object
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> object, ParseException e) {
                if (e == null) {}}

这正在从解析中正确检索数据。


0
投票

我找到了解决方案。您无法使用ParseUser.getCurrentUser()检索指针的对象。您必须通过在User表中查找userId等于ParseUser.getCurrentUser()。getObjectId()的用户来执行完整的ParseUser查询以检索当前用户。

 ParseQuery query = ParseUser.getQuery();
        query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
        query.findInBackground(new FindCallback() {
            @Override
            public void done(List<ParseObject> users, ParseException e) {
                if (e == null) for (userObject : users) {
                    // I retrieve the objectId from the pointer
                    // only after directly querying for the current User.
                    userObject.getParseObject("currentGroup").getObjectId(); 
                }
            }
        });

至少,这是我设法在我的应用程序中使用它的唯一方法。我不知道为什么.getParseObject()方法可用于ParseUser.getCurrentUser(),如果你不能直接检索指定指针对象的objectId,但是哦。如果有人知道,我很想知道你是怎么做到的。

© www.soinside.com 2019 - 2024. All rights reserved.