title: All components of a fragment returning null
body: <p>Okay, so I am trying pass a JSON Object, converted to a string to another intent using the putExtra property. Then the code below triggers to launch an activity to which I am trying to add a fragment. But the app crashes while trying to load that activity saying that a method was being cast on a null object. Help needed.</p>
  
  <p>Here's the code that triggers the Activity :</p>
  
  <pre><code>detailsField[i].setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {
                      Intent intent = new Intent(getContext() , DetailActivity.class);
                      intent.putExtra("jsonStr" , J.toString());
                      startActivity(intent);
                  }
              });
  </code></pre>
  
  <p>Here's the DetailActivity.java class :</p>
  
  <pre><code>public class DetailActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      setContentView(R.layout.activity_detail);
      DetailFragment hello = new DetailFragment();
      getSupportFragmentManager().beginTransaction().add(R.id.fragment_detail, hello, "HELLO").commit();
      }
  }
  </code></pre>
  
  <p>Here are the methods inside the fragment :</p>
  
  <pre><code>public void updateView() {
      try {
          obj = new JSONObject(intent.getStringExtra("jsonStr"));
          setWeatherIcon();
          humidityIcon.setText(getActivity().getString(R.string.humidity_icon));
          sunsetIcon.setText(getActivity().getString(R.string.sunset_icon));
          sunriseIcon.setText(getActivity().getString(R.string.sunrise_icon));
          speedIcon.setText(getActivity().getString(R.string.speed_icon));
          pressureIcon.setText(getActivity().getString(R.string.pressure_icon));
  
          Long date1 = obj.getLong("dt");
          Date expiry = new Date(Long.parseLong(Long.toString(date1)) * 1000);
          String date = new SimpleDateFormat("EE, dd" , Locale.US).format(expiry);
          dateText.setText(date);
  
          long dy = obj.getJSONObject("main").getLong("day");
          int day = (int) dy;
          sunriseText.setText("Day : " + day + getString(R.string.c));
  
          long nt = obj.getJSONObject("main").getLong("night");
          int night = (int) nt;
          sunsetText.setText("Night : " + night + getString(R.string.c));
  
          SpannableString ss1=  new SpannableString(date + "\n"
                  + obj.getJSONObject("temp").getLong("max") + "°" + "      "
                  + obj.getJSONObject("temp").getLong("min") + "°" + "\n");
          ss1.setSpan(new RelativeSizeSpan(1.1f), 0,7, 0); // set size
          ss1.setSpan(new RelativeSizeSpan(1.4f) , 8 , 11 , 0);
          tempText.setText(ss1);
  
          humidityText.setText(obj.getInt("humidity") + "%");
          speedText.setText(obj.getLong("speed") + " km/h");
          pressureText.setText(obj.getLong("pressure") + " hPa");
      }
      catch (JSONException ex) {
          Log.e("Detail View" , "Cannot Find Details");
          getActivity().finish();
      }
  }
  
  private void setWeatherIcon() throws JSONException {
      int id = obj.getJSONArray("weather").getJSONObject(0).getInt("id");
      String icon = "";
      switch(id) {
          /*Many Switch Cases above which do not cause problem*/
          case 900 :
          case 781 : icon = getActivity().getString(R.string.weather_tornado);
              break;
          case 904 :
          case 800 : icon = getActivity().getString(R.string.weather_sunny);
              break;
          case 801 :
          case 802 :
          /*Many Switch Cases below which do not cause problem*/
      }
      weatherIcon.setText(icon);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
      cityField = (TextView) rootView.findViewById(R.id.city_field);
      dateText = (TextView) rootView.findViewById(R.id.date_text);
  
      weatherIcon = (TextView) rootView.findViewById(R.id.weather_icon);
      weatherIcon.setTypeface(weatherFont);
      tempText = (TextView) rootView.findViewById(R.id.temp_text);
  
      humidityIcon = (TextView) rootView.findViewById(R.id.humidity_icon);
      humidityIcon.setTypeface(weatherFont);
      humidityText = (TextView) rootView.findViewById(R.id.humidity_text);
      speedIcon = (TextView) rootView.findViewById(R.id.speed_icon);
      speedIcon.setTypeface(weatherFont);
      speedText = (TextView) rootView.findViewById(R.id.speed_text);
      pressureIcon = (TextView) rootView.findViewById(R.id.pressure_icon);
      pressureIcon.setTypeface(weatherFont);
      pressureText = (TextView) rootView.findViewById(R.id.pressure_text);
  
      sunriseIcon = (TextView) rootView.findViewById(R.id.sunrise_icon);
      sunriseIcon.setTypeface(weatherFont);
      sunriseText = (TextView) rootView.findViewById(R.id.sunrise_text);
      sunsetIcon = (TextView) rootView.findViewById(R.id.sunset_icon);
      sunsetIcon.setTypeface(weatherFont);
      sunsetText = (TextView) rootView.findViewById(R.id.sunset_text);
  
      return rootView;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      weatherFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/newweather.ttf");
      intent = getActivity().getIntent();
      updateView();
  }
  </code></pre>
  
  <p>Here's the activity_detail.xml file, which is for the DetailActivity</p>
  
  <pre><code>&lt;android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context="com.a5corp.weather.DetailActivity"&gt;
  
  &lt;android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          app:popupTheme="@style/AppTheme.PopupOverlay" /&gt;
  
  &lt;/android.support.design.widget.AppBarLayout&gt;
  
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/fragment_detail"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.a5corp.weather.WeatherActivity"
      tools:ignore="MergeRootFrame"
      android:background="@color/colorPrimary" /&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>I don't know if I should actually provide the Fragment xml or not because I my fragment is well defined.</p>
  
  <p>Here is the error message :</p>
  
  <pre><code>12-21 23:33:23.777 13286-13286/com.a5corp.weather E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.a5corp.weather, PID: 13286
                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.a5corp.weather/com.a5corp.weather.DetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:153)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5441)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                      at com.a5corp.weather.DetailFragment.setWeatherIcon(DetailFragment.java:157)
                                                                      at com.a5corp.weather.DetailFragment.updateView(DetailFragment.java:37)
                                                                      at com.a5corp.weather.DetailFragment.onCreate(DetailFragment.java:196)
                                                                      at android.support.v4.app.Fragment.performCreate(Fragment.java:2075)
                                                                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1060)
                                                                      at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838)
                                                                      at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:861)
                                                                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719)
                                                                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
                                                                      at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                                      at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
                                                                      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
                                                                      at android.app.Activity.performStart(Activity.java:6320)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2386)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:153) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5441) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) 
  </code></pre>
  
  <p>(PS. Line 157 in the Fragment is where it says <code>weatherIcon.setText(icon)</code> in the setWeatherIcon() function. </p>
  
  <p>Line 37 is setWeatherIcon() being called inside the Fragment's updateView()</p>
  
  <p>Line 96 is updateView() being called inside the Fragment's onCreate() )</p>
  
  <p>(Even if I comment out the line, it gives the same NullPointerException for another TextView, commenting that one too gives another NullPointerException for another TextView)</p>