当我使用SupportStreetViewPanoramaFragment时,应用程序崩溃了。[英] Application getting crashed when i used SupportStreetViewPanoramaFragment

本文是小编为大家收集整理的关于当我使用SupportStreetViewPanoramaFragment时,应用程序崩溃了。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想在strretview中开始一些开发,并为我使用supportstreetViewPanoramAuragment.但是当我启动我的应用程序时,它将与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" />

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

推荐答案

omg !!!!它正在努力改变...

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

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

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

输出:

在此处输入图像描述

它真的很棒的令人惊叹的StreetView.

其他推荐答案

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

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

返回null,然后尝试使用npe结尾的getStreetViewPanorama().检查你的整个布局

本文地址:https://www.itbaoku.cn/post/102524.html

问题描述

I want to started some development in StrretView and for that i used SupportStreetViewPanoramaFragment. but when i start my application it's crash with below 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)

My Activity code is below:

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

Application got crashed at this line

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

My Layout is

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

Any help or idea would appreciated.

推荐答案

OMG!!!! It's working by just changing...

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

to

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

Hope it helps to others! I need to used SupportStreetViewPanoramaFragment

OutPut:

enter image description here

It's really amazing experience with StreetView.

其他推荐答案

You show no whole layout but you clearly crash because

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

returns null on which you then try getStreetViewPanorama() which ends with NPE. Check your whole layout