当我使用SupportStreetViewPanoramaFragment时,应用程序崩溃了

时间:2014-07-17 11:11:56

标签: android google-street-view

我想在StrretView中开始一些开发,为此我使用了SupportStreetViewPanoramaFragment。但是当我启动我的应用程序时,它会在logcat下面崩溃。

07-17 16:33:53.805: E/AndroidRuntime(19449): FATAL EXCEPTION: main
07-17 16:33:53.805: E/AndroidRuntime(19449): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stacky/com.example.stacky.SplitStreetViewPanorama}: java.lang.NullPointerException
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.os.Looper.loop(Looper.java:176)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.app.ActivityThread.main(ActivityThread.java:5419)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at java.lang.reflect.Method.invoke(Method.java:525)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at dalvik.system.NativeStart.main(Native Method)
07-17 16:33:53.805: E/AndroidRuntime(19449): Caused by: java.lang.NullPointerException
07-17 16:33:53.805: E/AndroidRuntime(19449):    at com.example.stacky.SplitStreetViewPanorama.setUpStreetViewPanoramaIfNeeded(SplitStreetViewPanorama.java:42)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at com.example.stacky.SplitStreetViewPanorama.onCreate(SplitStreetViewPanorama.java:36)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.app.Activity.performCreate(Activity.java:5372)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
07-17 16:33:53.805: E/AndroidRuntime(19449):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)

我的活动代码如下:

public class SplitStreetViewPanorama extends FragmentActivity
implements OnMarkerDragListener, OnStreetViewPanoramaChangeListener {

private StreetViewPanorama svp;
private GoogleMap mMap;
private Marker marker;

// George St, Sydney
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);

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

    setUpStreetViewPanoramaIfNeeded(savedInstanceState);
}

private void setUpStreetViewPanoramaIfNeeded(Bundle savedInstanceState) {
    if (svp == null) {
        svp = ((SupportStreetViewPanoramaFragment)getSupportFragmentManager().findFragmentById(R.id.streetviewpanorama)).getStreetViewPanorama();
        setUpMap();
        if (svp != null) {
            if (savedInstanceState == null) {
                svp.setPosition(SYDNEY);
            }
            svp.setOnStreetViewPanoramaChangeListener(this);
        }
    }
}

@Override
public void onStreetViewPanoramaChange(StreetViewPanoramaLocation location) {
    if (location != null) {
      marker.setPosition(location.position);
    }
}

private void setUpMap() {
    mMap.setOnMarkerDragListener(this);
    // Creates a draggable marker. Long press to drag.
    marker = mMap.addMarker(new MarkerOptions()
            .position(SYDNEY)
            .draggable(true));
}

@Override
public void onMarkerDragStart(Marker marker) {
}

@Override
public void onMarkerDragEnd(Marker marker) {
    svp.setPosition(marker.getPosition(), 150);
}

@Override
public void onMarkerDrag(Marker marker) {
}

应用程序在这一行崩溃了

  svp = ((SupportStreetViewPanoramaFragment)getSupportFragmentManager().findFragmentById(R.id.streetviewpanorama)).getStreetViewPanorama();

我的布局是

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

任何帮助或想法都会受到赞赏。

2 个答案:

答案 0 :(得分:7)

OMG !!!!这只是改变......

class="com.google.android.gms.maps.StreetViewPanoramaFragment"

class="com.google.android.gms.maps.SupportStreetViewPanoramaFragment"

希望对别人有所帮助!我需要使用SupportStreetViewPanoramaFragment

输出:

enter image description here

使用StreetView真的很棒。

答案 1 :(得分:1)

你没有显示整个布局,但你明显崩溃,因为

((SupportStreetViewPanoramaFragment)getSupportFragmentManager().findFragmentById‌​(R.id.streetviewpanorama))

返回null,然后您尝试使用以NPE结尾的getStreetViewPanorama()。检查整个布局