title: Android: Inflate Custom View inside Fragment
body: <p>I have an application that uses a fragment view as part of its layout. Within this fragment, I want to instantiate a Paint Canvas where i can do custom drawing. </p>
  
  <p>The problem is, my app crashes when I go to inflate the fragment with the error <code>android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.example.xxxxx.NeckCanvasOverlay</code></p>
  
  <p>I'm not sure what's causing my Paint class not to inflate properly. I know there are special rules about how you use findViewById inside a fragment, but I thought I was adhering to them since i'm calling findViewByid on the fragment's view itself (called "v" in my code). </p>
  
  <p>How can I fix this issue?</p>
  
  <p><strong>FragmentNeckDisplayMenu.java (Fragment Class)</strong></p>
  
  <pre><code>public class FragmentNeckDisplayMenu extends Fragment {
      private static View v;
      private NeckCanvasOverlay neckHUD;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          v = inflater.inflate(R.layout.menu_fragment_neck_display,container,false);      //ERROR HERE
          NeckCanvasOverlay neckHUD = (NeckCanvasOverlay) v.findViewById(R.id.neckHUD);   //paint test
          ...
          return v;
      }
  }
  </code></pre>
  
  <p><strong>NeckCanvasOverlay.java (Paint Class)</strong></p>
  
  <pre><code>class NeckCanvasOverlay extends View {
      private Paint mPainter;
  
      public NeckCanvasOverlay(Context context) {
          super(context);
          initView();
      }
  
      private void initView(){    //Initializes canvas &amp; paint objects here to save performance
          mPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
          mPainter.setColor(Color.BLUE);
          mPainter.setAlpha(128);
      }
  
      @Override
      public void onDraw(Canvas canvas) {
          super.onDraw(canvas);
          canvas.drawCircle(180, 900, 200, mPainter);
      }
  }
  </code></pre>
  
  <p><strong>menu_fragment_neck_display.xml (Fragment's Layout)</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:id="@+id/canvas"
      android:background="@drawable/image"&gt;
  
      &lt;LinearLayout
          android:layout_width="148dp"
          android:layout_height="0dp"
          android:layout_gravity="center"
          android:layout_weight="4"&gt;
          !--Custom view for Canvas here --&gt;
          &lt;com.example.xxxxx.NeckCanvasOverlay
              android:id="@+id/neckHUD"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:background="@drawable/neck_for_menu"/&gt;
      &lt;/LinearLayout&gt;
  
      &lt;Button
          android:id="@+id/menuIcon"
          android:layout_width="wrap_content"
          android:layout_height="0dp"
          android:layout_marginTop="64dp"
          android:text="Menu"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_gravity="bottom"
          android:layout_weight="0.5"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Full Stack Trace (Edit: Included full trace)</strong></p>
  
  <pre><code>   08-06 21:47:24.894 26334-26334/com.example.xxxxx E/ACRA: ACRA caught a InflateException for com.example.xxxxx
                                                               android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.example.gu.NeckCanvasOverlay
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
                                                                   at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32)
                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                   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.setOffscreenPageLimit(ViewPager.java:733)
                                                                   at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150)
                                                                   at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125)
                                                                   at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9)
                                                                   at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                   at android.os.AsyncTask.access$500(AsyncTask.java:180)
                                                                   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:158)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                   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)
                                                                Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.gu.NeckCanvasOverlay
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                                                                   at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32) 
                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
                                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
                                                                   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
                                                                   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
                                                                   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.setOffscreenPageLimit(ViewPager.java:733) 
                                                                   at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150) 
                                                                   at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125) 
                                                                   at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9) 
                                                                   at android.os.AsyncTask.finish(AsyncTask.java:651) 
                                                                   at android.os.AsyncTask.access$500(AsyncTask.java:180) 
                                                                   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                   at android.os.Looper.loop(Looper.java:158) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                                   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) 
                                                                Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.gu.NeckCanvasOverlay" on path: DexPathList[[zip file "/data/app/com.example.xxxxx-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.xxxxx-2/lib/arm64, /vendor/lib64, /system/lib64]]
                                                                   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:595)
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 
                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:855) 
                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:527) 
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                                                                   at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32) 
                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
                                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
                                                                   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
                                                                   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
                                                                   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.setOffscreenPageLimit(ViewPager.java:733) 
                                                                   at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150) 
                                                                   at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125) 
                                                                   at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9) 
                                                                   at android.os.AsyncTask.finish(AsyncTask.java:651) 
                                                                   at android.os.AsyncTask.access$500(AsyncTask.java:180) 
                                                                   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                   at android.os.Looper.loop(Looper.java:158) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                                   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) 
                                                                  Suppressed: java.lang.ClassNotFoundException: com.example.gu.NeckCanvasOverlay
                                                                   at java.lang.Class.classForName(Native Method)
                                                                   at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                   at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                          ... 33 more
                                                                Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
  08-06 21:47:24.954 26334-27123/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                     Process: com.example.xxxxx, PID: 26334
                                                     android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.example.gu.NeckCanvasOverlay
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
                                                         at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32)
                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                         at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                         at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                         at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                         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.setOffscreenPageLimit(ViewPager.java:733)
                                                         at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150)
                                                         at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125)
                                                         at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9)
                                                         at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                         at android.os.AsyncTask.access$500(AsyncTask.java:180)
                                                         at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:158)
                                                         at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                         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)
                                                      Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.gu.NeckCanvasOverlay
                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                                                         at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32) 
                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
                                                         at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
                                                         at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
                                                         at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
                                                         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.setOffscreenPageLimit(ViewPager.java:733) 
                                                         at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150) 
                                                         at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125) 
                                                         at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:9) 
                                                         at android.os.AsyncTask.finish(AsyncTask.java:651) 
                                                         at android.os.AsyncTask.access$500(AsyncTask.java:180) 
                                                         at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:158) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                         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) 
                                                      Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.gu.NeckCanvasOverlay" on path: DexPathList[[zip file "/data/app/com.example.xxxxx-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.xxxxx-2/lib/arm64, /vendor/lib64, /system/lib64]]
                                                         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                         at android.view.LayoutInflater.createView(LayoutInflater.java:595)
                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 
                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:855) 
                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:527) 
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                                                         at com.example.xxxxx.FragmentNeckDisplayMenu.onCreateView(FragmentNeckDisplayMenu.java:32) 
                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
                                                         at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
                                                         at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
                                                         at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
                                                         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.setOffscreenPageLimit(ViewPager.java:733) 
                                                         at com.example.xxxxx.PlayFrets.configMenu(PlayFrets.java:150) 
                                                         at com.example.xxxxx.AsyncTask_NoteFileLoader.onPostExecute(AsyncTask_NoteFileLoader.java:125) 
  </code></pre>