街景碎片黑屏

Street View Fragment black screen

我试图在 Activity 中插入街景片段,但我只收到黑屏,没有错误。

这是我的 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>

这是我的片段:

<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" />

这是我的主Activity 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) {
    }
}

这是我的 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);
    }
}

如果我直接在 Activity 中插入片段和 java 代码,同一个应用程序工作正常。错误在哪里?在这种情况下,我如何才能看到街景???

谢谢。

将您的 XML 从 fragment 更改为 com.google.android.gms.maps.StreetViewPanoramaView 并试一试。

您的代码中可能有 onCreateView,但您没有 post。如果您没有,则需要实施它。文档是 here.


更新:这里是quick demo of the fix. And a screen capture.

它可能对某人有用。但是,如果您构造一个未附加到视图的全景视图,那么您可能会遇到侦听器和黑屏问题。

我有一个膨胀视图来显示街景,我的全景视图附加到创建时的这个膨胀视图,然后将其设置为不可见并根据需要使用,然后我可以更改全景位置并更改其可见性。