title: Android Java | InflateException error inflating class when I call setContentView()
body: <p>I am new to Android. I am trying to make a simple app that creates a Pie Chart.</p>
  
  <p>I have 2 activities, one is MainActivity and the other is PieChart, both extending Activity, and I have a MyView class which extends View. </p>
  
  <p>In the MainActivity I have a button which is bound to a onClick event (set as a android:onClick="createPieChart" Button XML attribute) and when I click on it a function called 'creaTorta()' should change the activity and create the pie chart.  </p>
  
  <p>here is the MainActivity.java:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private View vista;
  
      public void creaPieChart(View myView){
         Intent viewPieChart = new Intent(this.getApplicationContext(), PieChart.class);
         startActivity(viewPieChart);
  
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         vista= (View)findViewById(R.id.viewtorta1);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
      }
  
  }
  </code></pre>
  
  <p>Here is the PieChart activity:</p>
  
  <pre><code> public class PieChart extends Activity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      Log.d("Point", "Here I am!");
  
              setContentView(R.layout.activity_la_torta);
      Log.d("Point", "Here I am again!");
  
     }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.la_torta, menu);
      return true;
      }
  
  }
  </code></pre>
  
  <p>And here is the MyView.java:</p>
  
  <pre><code>public class MyView extends View {
    private float cx=0,cy=0;
    private RectF r = new RectF(100,100,430,430);
    private float valori[] = {10,20,10,30,25,5};
        private Paint p,ct;
  
  public MyView(Context context) {
      super(context);
      p=new Paint();
      p.setAntiAlias(true);
      ct=new Paint();
      ct.setAntiAlias(true);
  }
  
     protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);
        float degStart=0,x,y;
        int i=0;
        ct.setColor(Color.argb(255, 255, 255, 255));
        for (float count : valori){
          float degVal=(float) (count*3.6);
          p.setColor(Color.argb(255, 0, 125, 255-i));
          x=(float)(Math.cos((((degStart-90))*Math.PI)/180)*200)+265 ;
          y=(float)(Math.sin((((degStart-90))*Math.PI)/180)*200)+265;
  
  
  
          canvas.drawArc(r, degStart-90 , degVal, true, p);
  
          canvas.drawText(""+degVal, x  ,y , ct);
  
          degStart+=degVal;
          i+=50;
      }
    } 
  
  } 
  </code></pre>
  
  <p>When I click on the button I get the following error: 
  FATAL EXCEPTION main
  java.lang.RuntimeException: Unable to start activity ComponentInfo()
  android.view.InflateException: Binary XML file Line #18: Error Inflating class it.adriano.piechart.MyView</p>
  
  <p>EDIT: Here is the activity_la_torta.xml file. The app crashes when I call the setContentView(R.layout.activity_la_torta) in the PieChart onCreate();</p>
  
  <pre><code> &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="300dp"
  android:layout_height="500dp"
  android:orientation="vertical"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"&gt;
  
  &lt;Button
      android:id="@+id/chiuditorta"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Button" /&gt;
  
  &lt;it.adriano.unatorta.MyView
      android:id="@+id/myView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" /&gt;
  
  &lt;View
      android:id="@+id/viewtorta1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here is the full stack trace (sorry for the indentation, if there's a way to post in a better way, please tell me):</p>
  
  <pre><code>03-14 13:41:52.009: E/AndroidRuntime(2189): 
  FATAL EXCEPTION: main
  03-14 13:41:52.009: E/AndroidRuntime(2189): 
  java.lang.RuntimeException: Unable to start activity ComponentInfo{it.adriano.unatorta/it.adriano.unatorta.LaTorta}:
   android.view.InflateException: Binary XML file line #18: 
  Error inflating class it.adriano.unatorta.MyView
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at 
  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at android.app.ActivityThread.access$600(ActivityThread.java:130)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at android.os.Handler.dispatchMessage(Handler.java:99)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at android.os.Looper.loop(Looper.java:137)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at android.app.ActivityThread.main(ActivityThread.java:4745)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at java.lang.reflect.Method.invokeNative(Native Method)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at java.lang.reflect.Method.invoke(Method.java:511)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     
  at dalvik.system.NativeStart.main(Native Method)
  03-14 13:41:52.009: E/AndroidRuntime(2189): 
  Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class it.adriano.unatorta.MyView
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.view.LayoutInflater.createView(LayoutInflater.java:596)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.app.Activity.setContentView(Activity.java:1867)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at it.adriano.unatorta.LaTorta.onCreate(LaTorta.java:21)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.app.Activity.performCreate(Activity.java:5008)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     ... 11 more
  03-14 13:41:52.009: E/AndroidRuntime(2189): Caused by: java.lang.NoSuchMethodException: &lt;init&gt; [class android.content.Context, interface android.util.AttributeSet]
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at java.lang.Class.getConstructor(Class.java:431)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
  03-14 13:41:52.009: E/AndroidRuntime(2189):     ... 22 more
  </code></pre>
  
  <p>How can I resolve. I do not understand why it doesn't start the startActivity(viewPieChart); properly. What am I messing?</p>
  
  <p>Thank you for the attention. </p>