WordPress WP_Query() Not working properly

a possible conflict with the name $posts which is used by wp core;
try, for instance:

$categories = get_categories();  
foreach($categories as $category)  
{    
  printf('<h2>%s</h2><ul>', $category->cat_name);   
  $cat_posts = new WP_Query('cat=".$category->cat_ID);   
  while($cat_posts->have_posts())   
  {          
    $cat_posts->the_post();     
    echo "<li>', the_title(), '</li>';    
  }      
  print '</ul>';   
}