title: Application crashes when I'm trying to set adapter
body: <p>I have one working ListView with JSON adapter, but when I'm trying to set another adapter on another listView my application crashes for unknown reason.</p>
  
  <p>I'm using TabLayout and PageAdapter.</p>
  
  <p>When I comment the following line (MainActivity.java, Method 1 below):</p>
  
  <pre><code>insListView.setAdapter(mJSONAdapterIns);
  </code></pre>
  
  <p>the application works.</p>
  
  <p>MainActivity getUpdates (Method 1) - WORKS - called in TabFragment1.java:</p>
  
  <pre><code>// 4. Access the ListView
                  mainListView = (ListView) findViewById(R.id.main_listview);
                  // 10. Create a JSONAdapter for the ListView
                  mJSONAdapter = new JSONAdapter(MainActivity.this, getLayoutInflater());
                  // Set the ListView to use the ArrayAdapter
                  mainListView.setAdapter(mJSONAdapter);
  </code></pre>
  
  <p>MainActivity getInstagram() -Method 2 - CRASHING - called in TabFragment2.java:</p>
  
  <pre><code>insListView = (ListView) findViewById(R.id.instagram_listview);
          // 5. Set this activity to react to list items being pressed
          //mainListView.setOnItemClickListener(MainActivity.this);
          // 10. Create a JSONAdapter for the ListView
          mJSONAdapterIns = new JSONAdapterIns(MainActivity.this, getLayoutInflater());
          // Set the ListView to use the ArrayAdapter
          insListView.setAdapter(mJSONAdapterIns); //Crashes if this line is NOT commented
  </code></pre>
  
  <p>I'm calling both methods from their Tab Fragments, using the MainActivity.</p>
  
  <p>TabFragment1.java - Works great:</p>
  
  <p>public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {</p>
  
  <pre><code>final View v = inflater.inflate(R.layout.fb_list, container, false);
  MainActivity king = (MainActivity)getActivity();
  king.getUpdates(); // Setting the first list adapter (Method 1)
  return v;
  </code></pre>
  
  <p>}</p>
  
  <p>The getUpdates() is updating my first listview and everything is fine.</p>
  
  <p>TabFragment2.java - calling the "crashing" method:</p>
  
  <pre><code>public class TabFragment2 extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          final View v = inflater.inflate(R.layout.instagram_list, container, false);
  
          MainActivity king = (MainActivity)getActivity();
          king.getInstagram(); //Causes crash in the setAdapter line
  
          return v;
      }
  }
  </code></pre>
  
  <p>Logcat error:</p>
  
  <pre><code>    1647-1647/il.co.test.test W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2cfab20)
  08-26 10:59:45.050    1647-1647/il.co.test.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: il.co.test.test, PID: 1647
      java.lang.NullPointerException
              at il.co.test.test.MainActivity.getInstagram(MainActivity.java:175)
              at il.co.test.test.TabFragment2.onCreateView(TabFragment2.java:20)
              at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
              at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
              at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:535)
              at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
              at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
              at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
              at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
              at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
              at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
              at android.view.Choreographer.doCallbacks(Choreographer.java:574)
              at android.view.Choreographer.doFrame(Choreographer.java:544)
              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
              at android.os.Handler.handleCallback(Handler.java:733)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5017)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
              at dalvik.system.NativeStart.main(Native Method)
  08-26 10:59:59.010    1647-1647/? I/Process﹕ Sending signal. PID: 1647 SIG: 9
  </code></pre>
  
  <p>How I can fix this crash?</p>
  
  <p>Edit 1:
  Was trying to add the following line in Method 2 and commenting the setAdapter.</p>
  
  <pre><code>TextView test = (TextView) findViewById(R.id.x1);
          test.setText("TEST");
  </code></pre>
  
  <p>Application crashed too.</p>
  
  <p>Seem like findViewById returns NULL. why?</p>
  
  <p><strong><em>instagram_list.xml</em></strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context=".MainActivity"
      android:layoutDirection="rtl"
      android:textDirection="rtl"
      android:background="#ffffff"&gt;
  
      &lt;TextView
          android:id="@+id/x1"
          android:text="TEST"
  
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  
  
      &lt;!-- List whose dataset is defined in code with an adapter --&gt;
      &lt;ListView
          android:id="@+id/instagram_listview"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:layout_marginTop="15dp"
          android:paddingStart="5dp"
          android:paddingEnd="5dp"
          android:layoutDirection="rtl"
          android:textDirection="rtl"
          android:divider="@android:color/transparent"
          android:dividerHeight="20.0sp"
  
          /&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>New logcat after Prerak's answer:</p>
  
  <pre><code>08-26 11:59:12.720    1599-1599/il.co.test.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: il.co.test.test, PID: 1599
      java.lang.IllegalStateException: System services not available to Activities before onCreate()
              at android.app.Activity.getSystemService(Activity.java:4532)
              at il.co.test.test.MainActivity.getInstagram(MainActivity.java:172)
              at il.co.test.test.TabFragment2.onCreateView(TabFragment2.java:25)
              at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
              at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
              at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:535)
              at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
              at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
              at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
              at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
              at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
              at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
              at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
              at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
              at android.view.Choreographer.doCallbacks(Choreographer.java:574)
              at android.view.Choreographer.doFrame(Choreographer.java:544)
              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
              at android.os.Handler.handleCallback(Handler.java:733)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5017)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
              at com.android.internal.os.ZygoteInit
  </code></pre>