我正在尝试在片段中显示街道视图。

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


这是我运行应用程序时收到的错误消息

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)


第33行是

initialize();


第43行是

streetFrag.getStreetViewPanoramaAsync(this);


我不确定是什么问题-您能帮忙吗?

最佳答案

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


该语句可能没有为StreetFrag创建对象。

检查streetFrag是否为null。

07-24 09:27