Retrieve Data frome firebase in AndroidStudio

mahedi hassan :

I find a error for retrieve data from firebase. I fetch the data from child("H_Location") from firebase database. when i hit the pickupButton in my phone then this app is closed automatically.

Hare is my Code in MngaddHospital Fragment:




import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.Objects;

public class MngAddHospitalFragment extends Fragment {

    private EditText hospital_name,hospital_email,hospital_number,h_lat,h_long;
    private Button pickButton,add_hospital_button;

    DatabaseReference href;

    //int PLACE_PICKER_REQUEST=1;




    public MngAddHospitalFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mng_add_hospital, container, false);

        hospital_name=(EditText) v.findViewById(R.id.hospital_name);
        hospital_email=(EditText) v.findViewById(R.id.hospital_email);
        hospital_number=(EditText) v.findViewById(R.id.hospital_number);
        h_lat=(EditText) v.findViewById(R.id.hospital_lat);
        h_long=(EditText) v.findViewById(R.id.hospital_long);

        pickButton=(Button) v.findViewById(R.id.btn_lat_long);
        add_hospital_button=(Button) v.findViewById(R.id.btn_add_hospital);

        pickButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                href= FirebaseDatabase.getInstance().getReference().child("H_Location");



                href.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        String longitude= Objects.requireNonNull(dataSnapshot.child("H_Location").getValue()).toString();
                        String latitude= Objects.requireNonNull(dataSnapshot.child("H_Location").getValue()).toString();

                        h_long.setText(longitude);
                        h_lat.setText(latitude);

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }
        });
return v;
}

firebase for H_Location

So the problem is when i click pickupButton in my phone then my phone close this app and goto home screen. So please give a solution to overcome this situation .

Thanks

Ashish :

You need to provide field name instead of your parent key

String longitude= Objects.requireNonNull(dataSnapshot.child("latitude").getValue(String.class));
String latitude= Objects.requireNonNull(dataSnapshot.child("longitude").getValue(String.class));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356698&siteId=1