Search code examples
androidandroid-fragmentsgoogle-street-view

Street View Fragment black screen


I am trying to to insert a Street View fragment inside an Activity but I receive only a black screen, without error.

This is my Activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.fennec.streetviewtest.MainActivity">

    <FrameLayout
        android:id="@+id/frameLayoutTest"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</android.support.constraint.ConstraintLayout>

This is my fragment:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/streetviewpanorama"
    android:name="com.google.android.gms.maps.SupportStreetViewPanoramaFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.fennec.streetviewtest.StreetViewFragment" />

This is my MainActivity java class:

public class MainActivity extends AppCompatActivity implements StreetViewFragment.OnFragmentInteractionListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Fragment fragment=new StreetViewFragment();
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.frameLayoutTest,fragment).commit();
    }
    @Override
    public void onFragmentInteraction(Uri uri) {
    }
}

And this is my StreetViewFragment java class:

public class StreetViewFragment extends Fragment implements OnStreetViewPanoramaReadyCallback {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public StreetViewFragment() {
    }

    public static StreetViewFragment newInstance(String param1, String param2) {
        StreetViewFragment fragment = new StreetViewFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    StreetViewPanorama mStreetViewPanorama;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        FragmentManager fm = getChildFragmentManager();
        SupportStreetViewPanoramaFragment streetViewPanoramaFragment =
                (SupportStreetViewPanoramaFragment) getFragmentManager()
                        .findFragmentById(R.id.streetviewpanorama);
        if (streetViewPanoramaFragment == null) {
            streetViewPanoramaFragment = streetViewPanoramaFragment.newInstance();
            fm.beginTransaction().replace(R.id.streetviewpanorama, streetViewPanoramaFragment).commit();
        }
        streetViewPanoramaFragment.getStreetViewPanoramaAsync(StreetViewFragment.this);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_street_view, container, false);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
        streetViewPanorama.setPosition(new LatLng(-33.87365, 151.20689));

    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

The same app works fine if I insert the fragment and the java code directly inside the Activity. Where is the error? How i can see StreetView in this case???

Thanks.


Solution

  • Change your XML from fragment to com.google.android.gms.maps.StreetViewPanoramaView and give it a try.

    You may have an onCreateView in your code, but you didn't post it. If you don't have it, you will need to implement it. Documentation is here.


    Update: Here is a quick demo of the fix. And a screen capture.