简体   繁体   English

在android studio中,我试图从firebase中获取文本并使用datasnapshot将其中继到一个片段中

[英]In android studio, I'm trying to take text from firebase and relay it into a fragment using datasnapshot

I was following a tutorial and got to this snippet of code and it crashes.我正在学习教程并获得了这段代码,但它崩溃了。 I can't figure out why.我不明白为什么。

I have used the debugger and it runs through this, however it says mPostId is null.我已经使用了调试器,它运行了这个,但是它说 mPostId 为空。 I have disabled the Instant run setting due to looking online and that didn't work.由于在线查看,我禁用了即时运行设置,但没有用。

public class ViewPostFragment extends Fragment {

    private static final String TAG = "ViewPostFragment";

    //widgets
    private TextView mContactSeller, mTitle, mDescription, mPrice, mLocation, mSavePost;
    private ImageView mClose, mWatchList, mPostImage;

    //vars
    private String mPostId;
    private Post mPost;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPostId = (String) getArguments().get(getString(R.string.arg_post_id));
        Log.d(TAG, "onCreate: got the post id: " + mPostId);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view_post, container, false);
        mContactSeller = (TextView) view.findViewById(R.id.post_contact);
        mTitle = (TextView) view.findViewById(R.id.post_title);
        mDescription = (TextView) view.findViewById(R.id.post_description);
        mPrice = (TextView) view.findViewById(R.id.post_price);
        mLocation = (TextView) view.findViewById(R.id.post_location);
        mClose = (ImageView) view.findViewById(R.id.post_close);
        mWatchList = (ImageView) view.findViewById(R.id.add_watch_list);
        mPostImage = (ImageView) view.findViewById(R.id.post_image);
        mSavePost = (TextView) view.findViewById(R.id.save_post);

        init();

        hideSoftKeyboard();

        return view;
    }

    private void hideSoftKeyboard(){
        final Activity activity = getActivity();
        final InputMethodManager inputManager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    private void init() {
        getPostInfo();
    }

    private void getPostInfo(){
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

        Query query = reference.child("posts")
                .orderByValue()
                .equalTo(mPostId);

        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                DataSnapshot singleSnapshot = dataSnapshot.getChildren().iterator().next();
                if(singleSnapshot != null){
                    mPost = singleSnapshot.getValue(Post.class);
                    Log.d(TAG, "onDataChange: found the post: " + mPost.getTitle());

                    mTitle.setText(mPost.getTitle());
                    mDescription.setText(mPost.getDescription());

                    String price = "FREE";
                    if(mPost.getPrice() != null){
                        price = "$" + mPost.getPrice();
                    }
                    mPrice.setText(price);
                    String location = mPost.getCity() + ", " + mPost.getState_province() + ", " +
                            mPost.getCountry();
                    mLocation.setText(location);
                    UniversalImageLoader.setImage(mPost.getImage(), mPostImage);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

Logcat逻辑猫

2019-04-04 10:18:23.852 21110-21110/ie.bs E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ie.bs, PID: 21110
    java.util.NoSuchElementException
        at java.util.Collections$EmptyIterator.next(Collections.java:4235)
        at com.google.firebase.database.DataSnapshot$1$1.next(com.google.firebase:firebase-database@@16.1.0:296)
        at com.google.firebase.database.DataSnapshot$1$1.next(com.google.firebase:firebase-database@@16.1.0:287)
        at ie.bs.ViewPostFragment$1.onDataChange(ViewPostFragment.java:99)

This is the line the error is pointing to in the getPostInfo() method.这是错误在 getPostInfo() 方法中指向的行。

DataSnapshot singleSnapshot = dataSnapshot.getChildren().iterator().next();

Thank you.谢谢你。

I know that this comes late but I hope it helps someone.我知道这来晚了,但我希望它可以帮助某人。

Try doing this instead inside your onDataChange method.尝试在您的 onDataChange 方法中执行此操作。

for(DataSnapshot snapshot: datasnapshot.getChildren() ){
   mPost = singleSnapshot.getValue(Post.class);
    //Handle your logic here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Android 工作室中使用 Google firebase 数据快照苦苦挣扎 - Struggling with Google firebase Datasnapshot in Android studio Android Studio Firebase 数据库数据快照返回 null - Android Studio Firebase Database DataSnapshot returning null DataSnapshot for 循环不附加 Firebase 数据库路径的子级(Android Studio) - Children of Firebase database path are not attached by DataSnapshot for loop (Android Studio) 如何使用android studio将片段中的图片或文本分享到Facebook? - How to share picture or text from fragment to Facebook using android studio? 我正在尝试在 Android Studio 中创建一个 Spinner - I'm trying to create a Spinner in Android Studio 我如何将 dataSnapshot 放入 HashMap 并迭代 Firebase Android - How do i put dataSnapshot into a HashMap and Iterate Firebase Android Android Firebase数据库datasnapshot从错误的数据库目录中提取信息 - Android Firebase db datasnapshot pulling information from wrong db directory (Android 和 Firebase)无法解析符号“dataSnapshot” - (Android & Firebase) Cannot resolve symbol 'dataSnapshot' 使用firebase实时数据库,我的条目大约有300个项目。 现在没有得到数据快照中firebase的响应 - Using firebase realtime database my entries are around 300 items. now not getting response from firebase in datasnapshot 尝试使用 Android Studio 连接到 Firebase 时出错 - Error while trying to connect to Firebase using Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM