1

I am trying to display a street view in a fragment.

public class StreetDisplay extends Fragment implements OnStreetViewPanoramaReadyCallback{

SupportStreetViewPanoramaFragment streetFrag;

static final LatLng PosOne = new LatLng(43.771925, -79.512460);

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.street_display, container, false);
    initialize();
    return v;

}

private void initialize() {

    streetFrag =
            (SupportStreetViewPanoramaFragment) getFragmentManager()
                    .findFragmentById(R.id.street);
    streetFrag.getStreetViewPanoramaAsync(this);
}

//this method is needed for using a ViewPager swiping feature
public static StreetDisplay newInstance(){
    StreetDisplay sd = new StreetDisplay();
    return sd;
}

@Override
public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
    panorama.setPosition(PosOne);
}
}

Here's the error message I get when I run my app

Process: com.example.nikhilbhaskar.mapplayground, PID: 22406
java.lang.NullPointerException
        at com.example.nikhilbhaskar.mapplayground.StreetDisplay.initialize(StreetDisplay.java:43)
        at com.example.nikhilbhaskar.mapplayground.StreetDisplay.onCreateView(StreetDisplay.java:33)

Line 33 is

initialize();

Line 43 is

streetFrag.getStreetViewPanoramaAsync(this);  

I am not sure what the problem is - Can you help?

Red Ghost
  • 302
  • 2
  • 12
  • Do you use support library for the `Fragment`? – Josef Feb 24 '15 at 22:16
  • 1
    By that you mean this? import android.support.v4.app.Fragment; Yes, this is the Fragment class I extend from – Red Ghost Feb 24 '15 at 22:24
  • Try changing this : `getFragmentManager()` to :`getSupportFragmentManager()` – Josef Feb 24 '15 at 22:28
  • But getSupportFragmentManager() is a method of the FragmentActivity class. I can't extend that class; I need Fragment classes since I am using these very same fragments in a ViewPager for screen-sliding animations – Red Ghost Feb 24 '15 at 23:28
  • You can use :`getActivity().getSupportFragmentManager();` – Josef Feb 24 '15 at 23:39
  • I tried this. Fixes my NullPointerException. But no street view. Gosh, no clue how to solve this. But Josef thanks a lot for your help :) – Red Ghost Feb 25 '15 at 00:17

3 Answers3

1
streetFrag =
            (SupportStreetViewPanoramaFragment) getFragmentManager()
                    .findFragmentById(R.id.street);

It is possible that this statement is not creating the object for StreetFrag.

Check whether is streetFrag is null or not.

Rahul Shah
  • 165
  • 2
  • 11
  • I checked just now. You are right. streetFrag is null and the statement does not create the object . Any idea why/how to fix it? – Red Ghost Feb 24 '15 at 21:57
  • This might help. Check this out.http://stackoverflow.com/questions/10833666/findfragmentbyid-always-returns-null – Rahul Shah Feb 24 '15 at 22:06
1

I tried the doc from official, and it worked perfectly.

My sample code:

StreetViewActivity

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
import com.google.android.gms.maps.StreetViewPanorama;
import com.google.android.gms.maps.StreetViewPanoramaFragment;
import com.google.android.gms.maps.model.LatLng;


public class StreetViewActivity extends FragmentActivity
        implements OnStreetViewPanoramaReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_street_view);
        StreetViewPanoramaFragment streetViewPanoramaFragment =
                (StreetViewPanoramaFragment) getFragmentManager()
                        .findFragmentById(R.id.streetviewpanorama);
        streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
    }


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

And for the activity_street_view.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context="com.bjiang.map_ex.StreetViewActivity">

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

</RelativeLayout>

For more details, please refer here.

EDIT:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
import com.google.android.gms.maps.StreetViewPanorama;
import com.google.android.gms.maps.SupportStreetViewPanoramaFragment;
import com.google.android.gms.maps.model.LatLng;

public class StreetDisplay extends FragmentActivity implements OnStreetViewPanoramaReadyCallback {

    static SupportStreetViewPanoramaFragment streetViewPanoramaFragment;

    static final LatLng PosOne = new LatLng(43.771925, -79.512460);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.street_display);
        streetViewPanoramaFragment =
                ((SupportStreetViewPanoramaFragment)getSupportFragmentManager().findFragmentById(R.id.street));

        streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
    }

    //this method is needed for using a ViewPager swiping feature
    public static Fragment newInstance(){
        return streetViewPanoramaFragment.newInstance();
    }

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

}
bjiang
  • 6,068
  • 2
  • 22
  • 35
  • bijiang, I tried this too and it worked perfectly for me. But my example is slightly different. My class extends Fragment not FragmentActivity because this fragment is used in a ViewPager (along with 2 other fragments) for the horizontal swiping/screen-slide feature in ViewPager. Example here: http://developer.android.com/training/animation/screen-slide.html – Red Ghost Feb 24 '15 at 22:17
  • I tried for an hour, but no luck, I post the best code i have tried in EDIT and [this](http://stackoverflow.com/questions/24801981/application-getting-crashed-when-i-used-supportstreetviewpanoramafragment) is the best I can find... – bjiang Feb 25 '15 at 00:09
  • Thanks bijiang, I will check out that link - I appreciate your help :) – Red Ghost Feb 25 '15 at 00:15
0

Within fragment use getChildFragmentManager() instead.

streetFrag = (SupportStreetViewPanoramaFragment) getChildFragmentManager()
                                                    .findFragmentById(R.id.street);
Code Lღver
  • 15,573
  • 16
  • 56
  • 75