title: Killing a background thread when a the fragment is destroyed
body: <p>i have an a fragment that have a background thread that update a progress bar value and change the progress bar drawable when the progress value reach a certain value when i try to start onther fragment in the same activity it case this error</p>
  
  <pre><code>    E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()' on a null object reference
      at com.example.mego.adas.ui.CarFragment$PotProgressThread$1.run(CarFragment.java:831)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:7409)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
  E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.mego.adas, PID: 25935
  java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()' on a null object reference
      at com.example.mego.adas.ui.CarFragment$PotProgressThread$1.run(CarFragment.java:831)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:7409)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
  </code></pre>
  
  <p>here is the code for the thread</p>
  
  <pre><code>    class PotProgressThread implements Runnable {
  
      @Override
      public void run() {
          while (potSensorValue &lt;= 1025 &amp;&amp; fragmentIsRunning) {
  
              if (fragmentIsRunning) {
  
                  // Update the progress bar
                  potProgressBarHandler.post(new Runnable() {
                      public void run() {
                          potProgressBar.setProgress((int) potSensorValue);
                          if (potSensorValue &gt;= 800) {
                              potProgressBar.setProgressDrawable(getActivity().
                                      getResources().getDrawable(R.drawable.progressbarred));
                          } else {
                              potProgressBar.setProgressDrawable(getActivity().
                                      getResources().getDrawable(R.drawable.progressbarblue));
  
                          }
                      }
                  });
                  try {
                      Thread.sleep(50);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              } else {
                  break;
              }
          }
      }
  }
  </code></pre>
  
  <p>Here where i cast start my thread in onCreateView() method</p>
  
  <pre><code>   @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
  
      View rootView = inflater.inflate(R.layout.fragment_car, container, false);
      initializeScreen(rootView);
  
      //check the internet connection
       connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
       networkInfo = connectivityManager.getActiveNetworkInfo();
  
      if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {
          buildGoogleApiClient();
  
          //set up the firebase
          mFirebaseDatabase = FirebaseDatabase.getInstance();
  
          //get the references for the childes
          //the main child for the directions services
          carDatabaseReference = mFirebaseDatabase.getReference().child(constant.FIREBASE_CAR);
  
          //the childes for the direction root
          connectionStateDatabaseReference = mFirebaseDatabase.getReference()
                  .child(constant.FIREBASE_CAR).child(constant.FIREBASE_CONNECTION_STATE);
  
          accidentStateDatabaseReference = mFirebaseDatabase.getReference()
                  .child(constant.FIREBASE_CAR).child(constant.FIREBASE_ACCIDENT_STATE);
  
          startStateStateDatabaseReference = mFirebaseDatabase.getReference()
                  .child(constant.FIREBASE_CAR).child(constant.FIREBASE_START_STATE);
  
          lightsStateDatabaseReference = mFirebaseDatabase.getReference()
                  .child(constant.FIREBASE_CAR).child(constant.FIREBASE_LIGHTS_STATE);
  
          lockStateDatabaseReference = mFirebaseDatabase.getReference()
                  .child(constant.FIREBASE_CAR).child(constant.FIREBASE_LOCK_STATE);
  
          mappingServicesDatabaseReference = mFirebaseDatabase.getReference()
                  .child(constant.FIREBASE_CAR).child(constant.FIREBASE_MAPPING_SERVICES);
  
          sensorsValuesDatabaseReference = mFirebaseDatabase.getReference()
                  .child(constant.FIREBASE_CAR).child(constant.FIREBASE_SENSORES_VALUES);
  
          connectionStateDatabaseReference.setValue(1);
  
  
      }
      //show Snackbar if there is no internet net connection
      else {
          if (carFragment != null) {
  
              //   Snackbar snackbar = Snackbar.make(carFragment, R.string.no_internet_connection, Snackbar.LENGTH_LONG);
              // snackbar.show();
          }
      }
  
      //get the date from the connected thread
      bluetoothHandler = new Handler() {
          public void handleMessage(android.os.Message msg) {
              //if message is what we want
              if (msg.what == handlerState) {
                  // msg.arg1 = bytes from connect thread
                  String readMessage = (String) msg.obj;
  
                  //keep appending to string until ~ char
                  recDataString.append(readMessage);
  
                  //determine the end of the line
                  endOfLineIndex = recDataString.indexOf("~");
                  if (fragmentIsRunning) {
                      refreshUI();
                  }
  
              }
          }
      };
  
      //setup ans start the threads
      potThread = new Thread(new PotProgressThread());
      tempThread = new Thread(new TemperatureProgressThread());
      ldrThread = new Thread(new LdrProgressThread());
  
  
      fragmentIsRunning = true;
      potThread.start();
      tempThread.start();
      ldrThread.start();
  
  
      //set the buttons listener
      lightsButton.setOnClickListener(this);
      lockButton.setOnClickListener(this);
      disconnectButton.setOnClickListener(this);
      startButton.setOnClickListener(this);
  
      //setup the map fragment
      MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.my_location_fragment_car);
      mapFragment.getMapAsync(this);
  
      // Inflate the layout for this fragment
      return rootView;
  }
  </code></pre>
  
  <p>Here is the code on onDestoryview() method</p>
  
  <pre><code>fragmentIsRunning = false;
  potThread.interrupt();
  </code></pre>
  
  <p>how can i kill it</p>