1

First off im very new to java and developing in general.

Im trying to show information (from Firebase) to a clickable pin in my google maps project. When i click the stored pin on the map i get following error: java.lang.IllegalStateException: Task is not yet complete.

This is the following code that makes the error:

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot dataValues : dataSnapshot.getChildren()) {
                    final Pin myPin = dataValues.getValue(Pin.class);
                    final String pinReference = dataValues.getKey();
                    try {
                        MarkerOptions pinMarker = new MarkerOptions().position(new LatLng(myPin.pinLocation.latitude, myPin.pinLocation.longitude));
                        pinMarker.icon(BitmapDescriptorFactory.fromResource(R.drawable.logo_pin));
                        mMap.addMarker(pinMarker);
                        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                            @Override
                            public boolean onMarkerClick(Marker marker) {
                                FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
                                Uri storageReference = firebaseStorage.getReference("pictures").child(pinReference + ".jpeg").getDownloadUrl().getResult();

                                Fragment fragment = new PinInfoFragment(myPin.placeName, firebaseUser.getEmail(), myPin.comment, storageReference, myPin.placeRating);
                                getSupportFragmentManager().beginTransaction().replace(R.id.include_center_fragment, fragment);
                                return true;
                            }
                        });
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }
                }

I have following code in a class fragment to display the information to the pin:

public class PinInfoFragment extends Fragment {

    String placeName;
    String name;
    String comment;
    Uri photoURL;
    double ratings;

    PinInfoFragment(String placeName, String name, String comment, Uri photoURL, double ratings){
        this.placeName = placeName;
        this.name = name;
        this.comment = comment;
        this.photoURL = photoURL;
        this.ratings = ratings;
    }

    TextView placeNameTextView;
    TextView nameTextView;
    TextView commentTextView;
    ImageView photoImageView;
    TextView ratingsTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_pin_info, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        placeNameTextView = getActivity().findViewById(R.id.pin_info_name);
        nameTextView = getActivity().findViewById(R.id.pin_info_name_user);
        commentTextView = getActivity().findViewById(R.id.pin_info_user_comment);
        photoImageView = getActivity().findViewById(R.id.pin_info_image);
        ratingsTextView = getActivity().findViewById(R.id.pin_info_rating);

        placeNameTextView.setText(placeName);
        nameTextView.setText(name);
        commentTextView.setText(comment);
        Glide.with(this).load(photoURL).into(photoImageView);
        ratingsTextView.setText("* " + ratings);

        getActivity().findViewById(R.id.pin_info_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().findViewById(R.id.pin_info_layout).setVisibility(View.GONE);
            }
        });

        getActivity().findViewById(R.id.pin_info_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

    }
}

Any help would be greatly appreciated!

Thanks.

HENRyyy
  • 25
  • 1
  • 4
  • 1
    Can you edit your question and add the stack trace of the error output (The output that had the "`java.lang.IllegalStateException`" text in it). So we can see what classes led up to the error. – Mark Stewart Dec 05 '19 at 18:52

1 Answers1

0

This code is causing the problem:

Uri storageReference = firebaseStorage
    .getReference("pictures")
    .child(pinReference + ".jpeg")
    .getDownloadUrl()
    .getResult();

getDownloadUrl doesn't return a URL immediately. It returns a Task that you need to listen to in order to get the result asynchronously. You can only call getResult on the task after it completes.

Please read the answer to this question to see how it works: How to get URL from Firebase Storage getDownloadURL

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441