街景片段黑屏

时间:2017-03-18 16:47:44

标签: android android-fragments google-street-view

我正在尝试在活动中插入街景视图片段,但我只收到黑屏,没有错误。

这是我的活动:

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

这是我的MainActivity java类:

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类:

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

如果我将片段和java代码直接插入Activity中,那么相同的应用程序工作正常。错误在哪里?在这种情况下我怎么能看到StreetView ???

感谢。

2 个答案:

答案 0 :(得分:0)

将您的XML从fragment更改为com.google.android.gms.maps.StreetViewPanoramaView,然后尝试一下。

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

更新:以下是此修复程序的quick demo。还有screen capture

答案 1 :(得分:0)

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

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