我在片段的onCreateView
方法中得到了一个NullPointerException
,但奇怪的是这只发生在模拟器上。在设备上,应用程序运行良好。
如果我为 NullPointerException 设置断点并在 eclipse 中调试,它会在以下行停止:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
mapTrackButton = (Button) rootView.findViewById(R.id.track_user_button);
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true); // This line
// More code
}
我认为谷歌地图在模拟器上不可用。
要修复错误,请执行以下操作:
if (map != NULL) {
map.setMyLocationEnabled(true);
// ...
}