我正在尝试在我的应用中使用Google的StreetView Image API,并将其与FragmentPageAdapter一起使用。我在一个活动中使用了streetview api并且工作正常但是当我在点击StreetViewPanoramaFragment片段时尝试“切换”到3片段fragmentPagerAdapter时,应用程序崩溃了以下内容:
Theme: themes:{}
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.StreetViewPanoramaFragment.getStreetViewPanoramaAsync(com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback)' on a null object reference
at xdesign.georgi.prototyping_espc_streetview.fragments.StreetViewFragment.onCreateView(StreetViewFragment.java:38)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:535)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
at xdesign.georgi.prototyping_espc_streetview.MainActivity.onTabSelected(MainActivity.java:94)
这是我的“StreetViewFragment”
public class StreetViewFragment extends Fragment implements OnStreetViewPanoramaReadyCallback {
private StreetViewPanoramaFragment streetViewPanoramaFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
StreetViewPanoramaFragment streetViewPanoramaFragment = (StreetViewPanoramaFragment) getActivity().getFragmentManager().findFragmentById(R.id.streetviewpanorama);
streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
return inflater.inflate(R.layout.street_virew_fragment_layout, container, false);
}
@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
streetViewPanorama.setPosition(new LatLng(-33.87365, 151.20689));
}
}
“street_virew_fragment_layout'xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.SupportStreetViewPanoramaFragment"
android:id="@+id/streetviewpanorama"
android:layout_width="match_parent"
android:layout_height="match_parent"/>