简体   繁体   English

如何从地图中的firestore检索数据

[英]How to retrieve the data from firestore in maps

Is there any way to retrieve the data from firestore in Maps.有什么方法可以从 Maps 的 firestore 中检索数据。 I am trying but its returning null here is my code我正在尝试,但它返回的 null 这是我的代码

docRef = db.collection("Buses").document("Bus1");
        Button btn=view.findViewById(R.id.submit);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        bus=documentSnapshot.getData();
                    }
                });
                if (!bus.get("ID").toString().matches(busId)) {
                    DocumentReference reference = db.collection("Buses").document("Bus2");
                    reference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                        @Override
                        public void onSuccess(DocumentSnapshot documentSnapshot) {
                            bus=documentSnapshot.getData();
                        }
                    });
                }
                if (bus!=null){
                    Toast.makeText(getContext(), "Data read Successfully", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(getContext(),MapsActivity.class);
                    startActivity(intent);

                    CONSTANTS.bus=HomeFragment.this.bus;
                    getActivity().finishAffinity();
                }
                else{
                    Toast.makeText(getContext(), "Data read Failed", Toast.LENGTH_SHORT).show();
                }

            }
        });

But I am getting NullPointerException here Here is the screenshot of the database但我在这里得到NullPointerException这是数据库的截图

强文本

I am not getting i why getdata function is returning null Here is the error log我不明白为什么getdata function 返回 null 这是错误日志

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference
        at 
com.integratedsoftsols.fyp.bustracking.user.HomeFragment$1.onClick(HomeFragment.java:53)
            at android.view.View.performClick(View.java:6274)
            at android.view.View$PerformClick.run(View.java:24859)
            at android.os.Handler.handleCallback(Handler.java:789)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6710)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

You are getting null because the database query is asynchronous and returns immediately, before the query is complete.你得到 null 因为数据库查询是异步的,并且在查询完成之前立即返回。 Your code continues to execute while the server performs the query, then the callback you provide to addOnSuccessListener is invoked some time later, whenever the query is complete.您的代码在服务器执行查询时继续执行,然后您提供给addOnSuccessListener的回调将在一段时间后调用,只要查询完成。

As such, the line of code where you log the result is being executed before the query is complete and bus has the value you expect.因此,在查询完成之前执行记录结果的代码行,并且bus具有您期望的值。 If you add logging at the line immediately after getData , within the callback (not after the callback), you will see what I mean.如果您在getData之后的行中立即添加日志记录,在回调中(而不是在回调之后),您将明白我的意思。

Your code will have to account for the asynchronous nature of the database calls.您的代码必须考虑数据库调用的异步性质。 You won't be able to use bus until after the query completes.在查询完成之前,您将无法使用bus

    docRef = db.collection("Buses").document("Bus1");
Button btn=view.findViewById(R.id.submit);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                bus=documentSnapshot.getData();
        if (!bus.get("ID").toString().matches(busId)) {
            DocumentReference reference = db.collection("Buses").document("Bus2");
            reference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    bus=documentSnapshot.getData();
                }
            });
        }
        if (bus!=null){
            Toast.makeText(getContext(), "Data read Successfully", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(getContext(),MapsActivity.class);
            startActivity(intent);

            CONSTANTS.bus=HomeFragment.this.bus;
            getActivity().finishAffinity();
        }
        else{
            Toast.makeText(getContext(), "Data read Failed", Toast.LENGTH_SHORT).show();
        }
        }
    });


    }
});

try this尝试这个

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM