title: Fragment in Activity is gets context null in setAdapter for ListView
body: <p>this is an issue that I've been having for like a 3 months, actually, since I started to develop this app. I've read hundreds of posts in this web site, in others, I've read tutorials and I haven't found a solution. <strong>I believe this problem has to do something with the way I have the structure of the app</strong>. </p>
  
  <p>I am developing an app that displays several sections, each section is a Fragment, and the main activity uses a ViewPager to display one fragment at a time(like the fb app or the twitter app). Each section will display listviews with different info: news, videos, classifieds, among other stuff. Once a section is displaying, let's say the news section, it shows a listview with the articles/news. The problem is when I try to display the listview, because in the Adapter I use to display the listview gives me NullPointerException, in the line <code>setAdapter</code>.</p>
  
  <p>I tracked down the line that gives me the exception, and is actually the <code>context</code> declaration. Even though the context is null in all my fragments( I don't know why), I'm able to display the listview, but in some others, the app crashes and gives the the NPE. I get the context through the line <code>Context context=getActivity()</code> but when I test this it gives me null. </p>
  
  <p>This is one of the fragments that I've succesfully displayed the listview </p>
  
  <pre><code>public class HomeActivity extends Fragment{
     static ListView newsList;
     static Item item=null;
     Context context=getActivity();
     ItemAdapter itemAdapter;
     List&lt;Item&gt; news=new ArrayList&lt;Item&gt;();
     static int i=0;
     static Activity activity;
     static String HOME_FRAGMENT="NEWS";
     public static final int TIME_ENTRY_REQUEST_CODE=1;
     FragmentManager fm;
  
  
  public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
     super.onCreateView(inflater, container, savedInstanceState);
     View view=inflater.inflate(R.layout.home_activity, container,false);
     newsList=(ListView)view.findViewById(R.id.newslist);
     setRetainInstance(true); 
     return view;
  }
  
  public void onAttach(Activity activity){
      super.onAttach(activity);
      HomeActivity.activity=activity;
  }
  
  public void onSaveInstanceState(Bundle outState){
      super.onSaveInstanceState(outState);
  }
  
  public void displayNews(final List&lt;Item&gt; news){
      try {        
           if(news==null || news.size()==0){
                  //Retrieve again the data list
           }
           Log.d("HomeActivity","total news: "+news.size());
       } catch (Exception e) {
           e.printStackTrace();
       }
      if(context==null){
           Log.d("HomeActivity","context null!");
      }
      itemAdapter=new ItemAdapter(context,(ArrayList&lt;Item&gt;) news);
      newsList.setAdapter(itemAdapter);
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>UPDATED: And this is another fragment where I get a NPE,</p>
  
  <pre><code>public class VideosActivity extends Fragment{
  
  ListView listvideo;
  ItemAdapter itemAdapter;
  Context context;
  Activity activity;
  public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
         super.onCreateView(inflater, container, savedInstanceState);
         View view=inflater.inflate(R.layout.videos_activity, container,false);
         listvideo=(ListView)view.findViewById(R.id.videoslist);
         Log.d("videos activity","onCreateView ready!!");
                 context=getActivity();
         setRetainInstance(true); 
         return view;
      }
  
  
  public void onAttach(Activity activity){
      super.onAttach(activity);
      this.activity=activity;
  }
  
  public void displayVideos(final List&lt;Item&gt; videos){
      try {        
           if(videos==null || videos.size()==0){
                  //Retrieve again the data list
  
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
      for(int i=0;i&lt;videos.size();i++){
  
      }
      itemAdapter=new ItemAdapter(activity,(ArrayList&lt;Item&gt;) videos);//I also used context in this line, but I still get the NPE
      listvideo.setAdapter(itemAdapter); //Here's the line that gives me the NPE
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>I am parsing all the info from an XML, and I'm managing everything through AsyncTask<br>
  <strong>This is my adapter</strong></p>
  
  <pre><code>public class ItemAdapter extends BaseAdapter implements OnItemClickListener{
      Item item=new Item();
      private ArrayList&lt;Item&gt; news=new ArrayList&lt;Item&gt;();
      private static final int NO_PICTURE_VIEW=0;
      private static final int PICTURE_VIEW=1;
  
      public ItemAdapter(Context context,ArrayList&lt;Item&gt; items) {
      super();
      news.addAll(items);
      }
      public int getCount() {
      // TODO Auto-generated method stub
      return news.size();
  }
  
      public Item getItem(int index) {
      // TODO Auto-generated method stub
      return getItem(index);
  }
  
      public long getItemId(int index) {
      // TODO Auto-generated method stub
      return index;
  }
  
      public int getViewTypeCount() {
           return 2;
  }
  
      public int getItemViewType(int position) {
      Item article=news.get(position);      
          if(article.getImage()!=null)      
              return PICTURE_VIEW;
          else
                  return NO_PICTURE_VIEW;
  }
  @Override
      public View getView(int index, View view, ViewGroup parent) {
      item=news.get(index);
      int type=getItemViewType(index);
      if(view==null){
              LayoutInflater inflater=LayoutInflater.from(parent.getContext());
          if(type==NO_PICTURE_VIEW){
          view=inflater.inflate(R.layout.item_no_picture_list,parent,false);
                  TextView titleView=(TextView)view.findViewById(R.id.titleNoPicture);
          titleView.setText(item.getTitle());
          }
          else{
          view=inflater.inflate(R.layout.item_picture_list,parent,false);
          TextView titleView=(TextView)view.findViewById(R.id.titleArticle);
          ImageView image=(ImageView)view.findViewById(R.id.imageItem);
          titleView.setText(item.getTitle());
          image.setImageBitmap(item.getImage());
      }
  }
  
  return view;
  }
  }
  </code></pre>
  
  <p><strong>This is the main activity</strong></p>
  
  <pre><code>public class MainActivity extends FragmentActivity{
      private String tabs[]={"News","Sports","Valley Life","Videos","Classifieds","Directory","Weather"};
  ViewPager viewPager=null;
  FragmentManager fragmentManager=getSupportFragmentManager();
  android.support.v4.app.FragmentTransaction ft=fragmentManager.beginTransaction();
      AdapterView adapterView;
  static ParseManager parser=new ParseManager();
  static List&lt;Item&gt; news=new ArrayList&lt;Item&gt;();
  ArrayList&lt;Item&gt; sports=new ArrayList&lt;Item&gt;();
  ArrayList&lt;Item&gt; valleyLife=new ArrayList&lt;Item&gt;();
  public static final int TIME_ENTRY_REQUEST_CODE=1;
  HomeActivity home;
  JSONManager jmanager=new JSONManager();
  WeatherManager weather=new WeatherManager();
  
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      viewPager=(ViewPager)findViewById(R.id.pager);
      viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
                  public void onPageSelected(int position)          
                  getActionBar().setSelectedNavigationItem(position);
              }
      });
      viewPager.setAdapter(new AdapterView(fragmentManager));
      viewPager.setOffscreenPageLimit(tabs.length);
      adapterView=new AdapterView(getSupportFragmentManager());
      /*initialization of the action bar: color, icon &amp; title.*/
      final android.app.ActionBar actionbar=getActionBar();
      ColorDrawable color=new ColorDrawable(Color.parseColor("#cd853f"));
      actionbar.setBackgroundDrawable(color);
      actionbar.setTitle("Title");
      ActionBar.TabListener tabListener=new ActionBar.TabListener() {
          public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  
          }   
  
  
          @Override
          public void onTabSelected(Tab tab, FragmentTransaction ft) {
              viewPager.setCurrentItem(tab.getPosition());
              ft=getFragmentManager().beginTransaction();
              //ft.replace(layouts[tab.getPosition()], (adapterView.getItem(tab.getPosition()));
              ft.commit();
  
          }
  
          @Override
          public void onTabReselected(Tab tab, FragmentTransaction ft) {  
  
          }
      };
      /*Displaying Tabs for the app */
      for(int i=0;i&lt;tabs.length;i++)  
          actionbar.addTab(actionbar.newTab().setText(tabs[i]).setTabListener(tabListener));
      actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
              viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                  @Override
                  public void onPageSelected(int position) {
                  actionbar.setSelectedNavigationItem(position);
              }
  
                  @Override
                  public void onPageScrolled(int arg0, float arg1, int arg2) {
              }
                  @Override
                  public void onPageScrollStateChanged(int arg0) {
              }
          });
          parser.execute();
          jmanager.execute();
          weather.execute();
          getApplicationContext();
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>This is my logcat </strong></p>
  
  <pre><code>FATAL EXCEPTION: main
  java.lang.NullPointerException
  at com.example.test23.VideosActivity.displayVideos(VideosActivity.java:54)
  at com.example.test23.ParseManager.onPostExecute(ParseManager.java:52)
  at com.example.test23.ParseManager.onPostExecute(ParseManager.java:1)
  at android.os.AsyncTask.finish(AsyncTask.java:631)
  at android.os.AsyncTask.access$600(AsyncTask.java:177)
  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5031)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>UPDATE: this is the video_activity.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" &gt;
  
          &lt;ListView 
              android:id="@+id/videoslist"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" &gt;
          &lt;/ListView&gt;
       &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I just want to be able to know what's wrong with my app and why I'm getting the context null, so I can proceed with what's next with my app, maybe is something with the structure, the way it's programmed, I don't know. Any help is appreciated, if you need more code, let me know.</p>