如何在其他活动中从Firebase检索数据?

时间:2018-08-29 18:44:14

标签: android firebase firebase-realtime-database

firebase db

我正在制作一个应用程序,其中我将数据保存在Firebase Realtime数据库上的RequestOrder的一个页面上,而我想在PendingOrder的另一页面上检索数据。

我正在使用TabLayout,其中片段一个标签显示PendingOrder,另一个标签显示完成的订单。经过大量的Google搜索后,我发现每个人都在使用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);


        }
    }

ServiceConsumer类

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;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将PkgDetail设置为NonNull或在其中保存一个空字符串。

private String PkgDetail = "" ;