簡體   English   中英

如何從 Firebase 檢索其他活動的數據?

[英]How to retrieve data from Firebase on other activity?

火力數據庫

我正在制作一個應用程序,在該應用程序中,我將數據保存在一個頁面上,該頁面是 Firebase 實時數據庫上的RequestOrder ,我想在另一個頁面上檢索它,即PendingOrder

我正在使用帶有片段的 tablayouts,其中一個選項卡顯示PendingOrder ,另一個顯示已完成的訂單。 經過大量谷歌搜索后,我發現每個人都在使用ListView從 Firebase 檢索數據。

我嘗試做同樣的事情,但陷入了錯誤。 請幫幫我。 錯誤是“java.lang.NullPointerException:嘗試在空對象引用上調用虛擬方法‘java.lang.String com.example.hp.another.ServiceConsumer.getPkgDetail()’”

public class PendingOrder extends Fragment {
    DatabaseReference databaseReference;
    FirebaseDatabase firebaseDatabase;
    FirebaseAuth.AuthStateListener mAuthListener;
    ListView listView1;
    String userId;
    List<ServiceConsumer> consumerList;
    private static final String TAG = "PendingOrder";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View RootView= inflater.inflate(R.layout.pendingorders, container, false);
        listView1 = (ListView) RootView.findViewById(R.id.listview);
        return RootView;

    }

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

        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = FirebaseDatabase.getInstance().getReference();
        FirebaseUser user = firebaseAuth.getCurrentUser();
        userId = user.getUid();
    }

    @Override
    public void onStart() {
        super.onStart();
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                showData(dataSnapshot);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void showData(DataSnapshot dataSnapshot) {
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            ServiceConsumer uInfo = new ServiceConsumer();
            uInfo.setPkgDetail(ds.child(userId).getValue(ServiceConsumer.class).getPkgDetail());
            uInfo.setPkgWeight(ds.child(userId).getValue(ServiceConsumer.class).getPkgWeight());
            uInfo.setPersonContct(ds.child(userId).getValue(ServiceConsumer.class).getPersonContct());
            uInfo.setPickupAddrs(ds.child(userId).getValue(ServiceConsumer.class).getPickupAddrs());
            uInfo.setConsumerName(ds.child(userId).getValue(ServiceConsumer.class).getConsumerName());
            uInfo.setConsumerPhone(ds.child(userId).getValue(ServiceConsumer.class).getConsumerPhone());
            uInfo.setConsumerAddres(ds.child(userId).getValue(ServiceConsumer.class).getConsumerAddres());
            Log.d(TAG, "showData:pkgdetail:" + uInfo.getPkgDetail());
            Log.d(TAG, "showData:pkgweight:" + uInfo.getPkgWeight());
            Log.d(TAG, "showData:persncntct:"+uInfo.getPersonContct());
            Log.d(TAG, "showData:pickup:"+uInfo.getPickupAddrs());
            Log.d(TAG, "showData:cnname:"+uInfo.getConsumerName());
            Log.d(TAG, "showData:cnphone:"+uInfo.getConsumerPhone());
            Log.d(TAG, "showData:cnadd:"+uInfo.getConsumerAddres());

            ArrayList<String>array= new ArrayList<>();
            array.add(uInfo.getPkgDetail());
            array.add(uInfo.getPkgWeight());
            array.add(uInfo.getPersonContct());
            array.add(uInfo.getPickupAddrs());
            array.add(uInfo.getConsumerName());
            array.add(uInfo.getConsumerAddres());
            array.add(uInfo.getConsumerPhone());
            ArrayAdapter<String> adapter= new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,array);
            listView1.setAdapter(adapter);


        }
    }

服務消費者類

public class ServiceConsumer {
    private  String Id;
    private String PkgDetail;
    private String PkgWeight;
    private String PersonContct;
    private String PickupAddrs;
    private  String ConsumerName;
    private String ConsumerPhone;
    private String ConsumerAddres;

    public ServiceConsumer() {
    }

    public ServiceConsumer(String id, String pkgdetail, String pkgweight, String personContct, String add, String consumerName, String consumerPhone, String consumerAdd) {
              Id= id;
        PkgDetail = pkgdetail;
        PkgWeight = pkgweight;
        PersonContct = personContct;
        PickupAddrs = add;
        ConsumerName = consumerName;
        ConsumerPhone = consumerPhone;
        ConsumerAddres = consumerAdd;

    }

    public String getId() {
        return Id;
    }

    public String getPkgDetail() {
        return PkgDetail;
    }

    public String getPkgWeight() {
        return PkgWeight;
    }

    public String getPersonContct() {
        return PersonContct;
    }

    public String getPickupAddrs() {
        return PickupAddrs;
    }

    public String getConsumerName() {
        return ConsumerName;
    }

    public String getConsumerPhone() {
        return ConsumerPhone;
    }

    public String getConsumerAddres() {
        return ConsumerAddres;
    }

    public void setId(String id) {
        Id = id;
    }

    public void setPkgDetail(String pkgDetail) {
        PkgDetail = pkgDetail;
    }

    public void setPkgWeight(String pkgWeight) {
        PkgWeight = pkgWeight;
    }

    public void setPersonContct(String personContct) {
        PersonContct = personContct;
    }

    public void setPickupAddrs(String pickupAddrs) {
        PickupAddrs = pickupAddrs;
    }

    public void setConsumerName(String consumerName) {
        ConsumerName = consumerName;
    }

    public void setConsumerPhone(String consumerPhone) {
        ConsumerPhone = consumerPhone;
    }

    public void setConsumerAddres(String consumerAddres) {
        ConsumerAddres = consumerAddres;
    }
}

嘗試使 PkgDetail 為 NonNull 或向其保存一個空字符串。

private String PkgDetail = "" ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM