简体   繁体   中英

StreeViewPanorama Api v2 returns null

im trying to implement a street view on my android application and have followed the documentation and Google API instructions...It complies fines and has no problems but when trying to run it on my android application it crashes due to not being able to carry out this line

 streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);

My Google play services, permissions and API key are all correct as I made a map beforehand and just commented it out to try this out but it do-sent seem to work and can't figure out why

    public class Maps extends Activity implements OnStreetViewPanoramaReadyCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    /*GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapping)).getMap();
    LatLngBounds bounds = new LatLngBounds(
            new LatLng(54.001615, -2.794561), // South west corner
            new LatLng(54.001615, -2.782674)); // North east corner
    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(54.00908, -2.787565), 14));
    map.addMarker(new MarkerOptions()
            .title("Lancaster Campus")
            .snippet("LUCM")
            .position(new LatLng(40.716216, -74.213393)));
    CameraPosition INIT =
            new CameraPosition.Builder()
                    .target(new LatLng(54.00908, -2.787565))
                    .zoom(25.5F)
                    .bearing(300F) // orientation
                    .tilt(50F) // viewing angle
                    .build();
    map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    map.setTrafficEnabled(true);
    map.setBuildingsEnabled(true);*/

    StreetViewPanoramaFragment streetViewPanoramaFragment =
            ((StreetViewPanoramaFragment) getFragmentManager()
                    .findFragmentById(R.id.streetviewpanorama));
    streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);

}

public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
    panorama.setPosition(new LatLng(-33.87365, 151.20689));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_maps, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


 }

fragment

 <fragment
android:name="com.google.android.gms.maps.StreetViewPanoramaFragment"
android:id="@+id/streetviewpanorama"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />

activity xml linked to class

 <?xml version="1.0" encoding="utf-8"?>
 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapping"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>

manifest meta data

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="*my api key*" />

the error

Caused by: 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 harman.com.lufr.Maps.onCreate(Maps.java:68)

streetViewPanoramaFragment is null.

Are you sure that you have added fragment to your xml and your id matches R.id.streetviewpanorama ?

<fragment
    android:name="com.google.android.gms.maps.StreetViewPanoramaFragment"
    android:id="@+id/streetviewpanorama"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

EDIT:

StreetViewPanoramaFragment is missing from your activity xml.

This worked for me.

streetViewPanoramaFragment.getStreetViewPanoramaAsync(
    new OnStreetViewPanoramaReadyCallback() {
        @Override
        public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {   
            // Do what you want to do here.
        }
    }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM