title: android xml, crash of the custom view in a layout
body: <p>I designed a simple compass app,
  at the first step, I wrote the app with a single view(the compass view) and it worked just fine. I made a new instance of my custom compass-view, and I used setcontent(mView).</p>
  
  <p>now im trying to use layouts, and I just cant make it work! everytime i try, the app crashes.</p>
  
  <p>here is the code:</p>
  
  <h2>the custom-view:</h2>
  
  <pre><code>public class CustomDrawableView extends View {
  public float azimut;
  Paint paint = new Paint();
  public CustomDrawableView(Context context) {
      super(context);
      paint.setStyle(Style.STROKE);
      paint.setStrokeWidth(2);
      paint.setColor(Color.WHITE);
      paint.setAntiAlias(true);
      azimut=0;
    };
  public CustomDrawableView(Context context, float Azimut) {
    super(context);
    paint.setStyle(Style.STROKE);
    paint.setStrokeWidth(2);
    paint.setColor(Color.WHITE);
    paint.setAntiAlias(true);
    azimut=Azimut;
  };
  protected void onDraw(Canvas canvas) {
      Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.compassarrowbitmap);
      int width = getWidth();
      int height = getHeight();
      int centerx = width/2;
      int centery = height/2;
      int imHeight=200;
      int imWidth=200;
      Bitmap arrow = Bitmap.createScaledBitmap(bMap,imHeight , imWidth, false);
      canvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated.
      canvas.rotate(-azimut*360/(2*3.14159f), centerx, centery);
      canvas.drawBitmap(arrow, centerx-imWidth/2, centery-imHeight/2, null);
      canvas.restore();           
    }
  }
  </code></pre>
  
  <h2>the xml-file:</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  &gt;
  &lt;FrameLayout 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"&gt;
  
  &lt;org.example.compassTest.CustomDrawableView
      android:id="@+id/compassview"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" /&gt;
  &lt;/FrameLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <h2>the main activity:</h2>
  
  <pre><code>public class MainActivity extends Activity implements SensorEventListener {
  Float azimut=(float) 0;  // View to draw a compass
  CustomDrawableView mCompassView;    
  private SensorManager mSensorManager;
  Sensor accelerometer;
  Sensor magnetometer;
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      this.mCompassView = (CustomDrawableView) findViewById(R.id.compassview);
      mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
      accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  }
  protected void onResume() {
      super.onResume();
      mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
      mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
  }
  protected void onPause() {
      super.onPause();
      mSensorManager.unregisterListener(this);
  }
  
  public void onAccuracyChanged(Sensor sensor, int accuracy) {  }
  
  float[] mGravity;
  float[] mGeomagnetic;
  public void onSensorChanged(SensorEvent event) {
      if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
          mGravity = event.values;
      if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
              mGeomagnetic = event.values;
      if (mGravity != null &amp;&amp; mGeomagnetic != null) {
          float R[] = new float[9];
          float I[] = new float[9];
          boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
          if (success) {
              float orientation[] = new float[3];
              SensorManager.getOrientation(R, orientation);
              azimut = orientation[0]; // orientation contains: azimut, pitch and roll
          }
      }
      this.mCompassView.azimut=azimut;
      this.mCompassView.invalidate();
  }
  }
  </code></pre>
  
  <p>*<em>*</em> log:</p>
  
  <pre><code>    01-07 04:40:54.957: W/dalvikvm(18101): threadid=1: thread exiting with uncaught exception (group=0x40c431f8)
  01-07 04:40:54.957: E/AndroidRuntime(18101): FATAL EXCEPTION: main
  **01-07 04:40:54.957: E/AndroidRuntime(18101): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.compasstest/org.example.compasstest.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class org.example.compassTest.CustomDrawableView****
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.os.Handler.dispatchMessage(Handler.java:99)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.os.Looper.loop(Looper.java:137)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.app.ActivityThread.main(ActivityThread.java:4507)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at java.lang.reflect.Method.invokeNative(Native Method)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at java.lang.reflect.Method.invoke(Method.java:511)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at dalvik.system.NativeStart.main(Native Method)
  01-07 04:40:54.957: E/AndroidRuntime(18101): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class org.example.compassTest.CustomDrawableView
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:271)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.app.Activity.setContentView(Activity.java:1835)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at org.example.compasstest.MainActivity.onCreate(MainActivity.java:28)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.app.Activity.performCreate(Activity.java:4465)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    ... 11 more
  01-07 04:40:54.957: E/AndroidRuntime(18101): Caused by: java.lang.ClassNotFoundException: org.example.compassTest.CustomDrawableView
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
  01-07 04:40:54.957: E/AndroidRuntime(18101):    ... 22 more
  </code></pre>