title: SeekBar listenner crashes app when in a fragment
body: <p>I have a seekbar inside a fragment and when I run the app it crashes. The fragment is part of the main activity. Here is the code:</p>
  <pre><code>public class PalleteFragment extends Fragment {
  
    public TextView textView;
    private CanvasView canvas;
  
    public PalleteFragment() {
      // Required empty public constructor
    }
  
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_pallete, container, false);
    }
  
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
  
      SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar);
      textView = (TextView) view.findViewById(R.id.textView);
      textView.setText(&quot;Pen size: &quot; + seekBar.getProgress());
  
      seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  
          @Override
          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
              canvas.setStrokeWidth(seekBar.getProgress());
              textView.setText(&quot;Pen size: &quot; + seekBar.getProgress());
          }
  
          ...
      });
    }
  }
  </code></pre>
  <p>Is something missing that I need to write?</p>
  <p>Here is the error that occurs when the app crashes:
  <a href="https://i.stack.imgur.com/YRIan.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YRIan.png" alt="enter image description here" /></a></p>
  <p>This is the CanvasView class in short:</p>
  <pre><code>public class CanvasView extends View implements View.OnTouchListener {
  
  
  private Paint mPaint;
  private Path mPath;
  private int mDrawColor;
  private int mBackgroundColor;
  private Canvas mExtraCanvas;
  private Bitmap mExtraBitmap;
  private int strokeWidth;
  
  private GestureDetector mGestureDetector;
  
  private static final float TOUCH_TOLERANCE = 4;
  private float mX, mY;
  
  CanvasView(Context context) {
  
      super(context);
      setOnTouchListener(this);
      strokeWidth = 10;
  
      mBackgroundColor = ResourcesCompat.getColor(getResources(),
              R.color.simpleWhite, null);
      mDrawColor = ResourcesCompat.getColor(getResources(),
              R.color.simpleBlack, null);
  
      mPath = new Path();
  
      mPaint = new Paint();
      mPaint.setColor(mDrawColor);
  
      mPaint.setAntiAlias(true);
  
      mPaint.setDither(true);
  
      mPaint.setStyle(Paint.Style.STROKE); // default: FILL
      mPaint.setStrokeJoin(Paint.Join.ROUND); // default: MITER
      mPaint.setStrokeCap(Paint.Cap.ROUND); // default: BUTT
      mPaint.setStrokeWidth(strokeWidth);
  }
  
  
  public CanvasView(Context context, AttributeSet attributeSet,GestureDetector mGestureDetector) {
      super(context);
  
      this.mGestureDetector = mGestureDetector;
      strokeWidth = 10;
  
      setOnTouchListener(this);
      mBackgroundColor = ResourcesCompat.getColor(getResources(),
              R.color.simpleWhite, null);
      mDrawColor = ResourcesCompat.getColor(getResources(),
              R.color.simpleBlack, null);
  
      mPath = new Path();
  
      mPaint = new Paint();
      mPaint.setColor(mDrawColor);
  
      mPaint.setAntiAlias(true);
      mPaint.setDither(true);
  
      mPaint.setStyle(Paint.Style.STROKE); // default: FILL
      mPaint.setStrokeJoin(Paint.Join.ROUND); // default: MITER
      mPaint.setStrokeCap(Paint.Cap.ROUND); // default: BUTT
      mPaint.setStrokeWidth(strokeWidth);
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
  
      canvas.drawBitmap(mExtraBitmap, 0, 0, null);
      mPaint.setStrokeWidth(strokeWidth);
  
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      float x = event.getX();
      float y = event.getY();
  
      switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              touchStart(x, y);
              updateColor(PreferenceManager.getDefaultSharedPreferences(getContext()).getInt(&quot;color&quot;,getResources().getColor(R.color.simpleBlack)));
              setStrokeWidth(PreferenceManager.getDefaultSharedPreferences(getContext()).getInt(&quot;stroke&quot;, 0));
              break;
          case MotionEvent.ACTION_MOVE:
              touchMove(x, y);
              invalidate();
              break;
          case MotionEvent.ACTION_UP:
              touchUp();
              break;
          default:
              // Do nothing.
      }
      return true;
  }
  
  private void touchStart(float x, float y) {
      mPath.moveTo(x, y);
      mX = x;
      mY = y;
  }
  
  private void touchMove(float x, float y) {
      float dx = Math.abs(x - mX);
      float dy = Math.abs(y - mY);
      if (dx &gt;= TOUCH_TOLERANCE || dy &gt;= TOUCH_TOLERANCE) {
          mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
          mX = x;
          mY = y;
          mExtraCanvas.drawPath(mPath, mPaint);
      }
  }
  
  private void touchUp() {
      mPath.reset();
  }
  
  @Override
  public boolean onTouch(View v, MotionEvent event) {
      mGestureDetector.onTouchEvent(event);
      return false;
  }
  
  public void erase(){
      mExtraCanvas.drawColor(ResourcesCompat.getColor(getResources(),
              R.color.simpleWhite, null));
  }
  
  public void setStrokeWidth(int width) {
      mPaint.setStrokeWidth(width);
  }
  </code></pre>
  <p>}</p>
  <p>The class is super long so I ommited other methods that weren't necessary for this question.</p>
  <p>Here is the XML from the fragment:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
     android:layout_width=&quot;match_parent&quot;
     android:layout_height=&quot;match_parent&quot;
     xmlns:tools=&quot;http://schemas.android.com/tools&quot;
     android:orientation=&quot;vertical&quot;&gt;
  
      &lt;LinearLayout
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:orientation=&quot;vertical&quot;&gt;
          &lt;SeekBar
              android:id=&quot;@+id/seekBar&quot;
              android:layout_width=&quot;150dp&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_weight=&quot;1&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/textView&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_weight=&quot;1&quot;
              android:text=&quot;Pen Size: &quot; /&gt;
      &lt;/LinearLayout&gt;
  </code></pre>
     
  <p>Here is the app's directories:</p>
  <p><a href="https://i.stack.imgur.com/k2dbz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k2dbz.png" alt="enter image description here" /></a></p>