title: Android - How to choose context?
body: <p>I have an issue with a finger painting program. In this program I implement two fragments, and I have come to realize that my drawing fragment, which is to contain a canvas and support drawing onto it, is not displaying the canvas, and has other problems. My realization is tied to my noticing that while I wrote a new class to extend View (done because the fragment would not support extending view itself), I did not instantiate the class. I have made an instance of the class within the fragment, but this instance requires a Context as a parameter, and I'm not sure what context to use. I tried using 'getActivity()' as the context, but that did not work and made the program hang (in debug) or crash (in normal mode). What should I be using for the context of this class instantiation? </p>
  
  <p>Fragment Code:</p>
  
  <pre><code>package com.example.chris.drawingtest;
  
  import android.app.Activity;
  import android.app.Fragment;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.Path;
  import android.graphics.Point;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Display;
  import android.view.LayoutInflater;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.WindowManager;
  import android.widget.ImageButton;
  import android.widget.LinearLayout;
  
  import com.example.chris.drawingtest.R;
  
  /**
   * Created by Chris on 11/28/2014.
   */
  public class DrawingFragment extends Fragment {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          Log.d("onCreateView: ","This ran successfully");
          return inflater.inflate(R.layout.fragment_drawing, container, false);
      }
  
      private class DrawView extends View {
  
          private Path drawpath = new Path();
          private Paint drawpaint = new Paint();
          private Paint canvaspaint;
          private Canvas drawcanvas;
          private Bitmap canvasBitmap;
  
          private int paintColor = 0xFF000000;    //opaque black for pencil
          private int canvasColor = 0xFFFFFFFF;   //pure white for canvas
  
          public DrawView(Context context) {
              super(context);
  
              Log.d("DrawView: ", "method is called");
  
              drawpaint.setColor(paintColor);
  
              drawpaint.setStrokeWidth(20);
              drawpaint.setStyle(Paint.Style.STROKE);
              drawpaint.setStrokeJoin(Paint.Join.ROUND);
              drawpaint.setStrokeCap(Paint.Cap.ROUND);
  
              canvaspaint = new Paint(Paint.DITHER_FLAG);
  
              Point p = getScreenSize(context);
              int w = p.x;
              int h = p.y;
  
              OnSizeChanged(w,h,0,0);
  
          }
  
          protected Point getScreenSize(Context context) {
              WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
              Display display = wm.getDefaultDisplay();
              Point size = new Point();
              display.getSize(size);
              return size;
          }
  
          protected void OnSizeChanged(int w, int h, int oldw, int oldh) {
              super.onSizeChanged(w,h,oldw,oldh);
  
              Log.d("OnSizeChanged: ", "method is called");
  
              canvasBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
              drawcanvas = new Canvas(canvasBitmap);
          }
  
          @Override
          protected void onDraw(Canvas canvas) {
              super.onDraw(canvas);
  
              Log.d("onDraw: ", "method is called");
  
              drawcanvas.drawBitmap(canvasBitmap,0,0,canvaspaint);
              canvas.drawPath(drawpath, drawpaint);
          }
  
          @Override
          public boolean onTouchEvent(MotionEvent event) {
              float touchX = event.getX();
              float touchY = event.getY();
  
              Log.d("onTouchEvent: ", "method is called");
  
              switch(event.getAction()) {
                  case MotionEvent.ACTION_DOWN:
                      drawpath.moveTo(touchX,touchY);
                      break;
                  case MotionEvent.ACTION_MOVE:
                      drawpath.lineTo(touchX,touchY);
                      break;
                  case MotionEvent.ACTION_UP:
                      drawcanvas.drawPath(drawpath, drawpaint);
                      drawpath.reset();
                      break;
                  default:
                      return false;
              }
              invalidate();
              return true;
          }
      }
  
      //private DrawView drawView = new DrawView();    this is the problematic instantiation
  
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> I wanted to add some new content to reflect answers given. </p>
  
  <p>Following the ideas put forth by Bruce, I expanded upon XML file for DrawingFragment. Unfortunately, this led to further crashes, no doubt with some of my own naivety to blame. Can anyone share some insight on this issue?</p>
  
  <p>logcat:</p>
  
  <pre><code>12-01 15:38:50.116    8045-8045/com.example.chris.drawingtest D/onCreateView:﹕ This ran successfully
  12-01 15:38:50.126    8045-8045/com.example.chris.drawingtest D/AndroidRuntime﹕ Shutting down VM
  12-01 15:38:50.126    8045-8045/com.example.chris.drawingtest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41c9aba8)
  12-01 15:38:50.126    8045-8045/com.example.chris.drawingtest E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.example.chris.drawingtest, PID: 8045
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chris.drawingtest/com.example.chris.drawingtest.DrawingActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.chris.drawingtest.DrawingFragment$DrawView
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5001)
              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:785)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.chris.drawingtest.DrawingFragment$DrawView
              at android.view.LayoutInflater.createView(LayoutInflater.java:603)
              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
              at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
              at com.example.chris.drawingtest.DrawingFragment.onCreateView(DrawingFragment.java:32)
              at android.app.Fragment.performCreateView(Fragment.java:1700)
              at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
              at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
              at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
              at android.app.Activity.onCreateView(Activity.java:4786)
              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
              at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
              at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
              at android.app.Activity.setContentView(Activity.java:1929)
              at com.example.chris.drawingtest.DrawingActivity.onCreate(DrawingActivity.java:18)
              at android.app.Activity.performCreate(Activity.java:5231)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5001)
              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:785)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NoSuchMethodException: &lt;init&gt; [class android.content.Context, interface android.util.AttributeSet]
              at java.lang.Class.getConstructorOrMethod(Class.java:472)
              at java.lang.Class.getConstructor(Class.java:446)
              at android.view.LayoutInflater.createView(LayoutInflater.java:568)
              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
              at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
              at com.example.chris.drawingtest.DrawingFragment.onCreateView(DrawingFragment.java:32)
              at android.app.Fragment.performCreateView(Fragment.java:1700)
              at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
              at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
              at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
              at android.app.Activity.onCreateView(Activity.java:4786)
              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
              at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
              at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
              at android.app.Activity.setContentView(Activity.java:1929)
              at com.example.chris.drawingtest.DrawingActivity.onCreate(DrawingActivity.java:18)
              at android.app.Activity.performCreate(Activity.java:5231)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5001)
              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:785)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Fragment XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".com.example.chris.drawingtest.DrawingFragment"
      &gt;
  
      &lt;view
          xmlns:android="http://schemas.android.com/apk/res/android"
          class="com.example.chris.drawingtest.DrawingFragment$DrawView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
  
  
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>