android xml,布局中自定义视图的崩溃

时间:2013-01-11 20:24:01

标签: android xml layout view

我设计了一个简单的罗盘应用, 在第一步,我用一个视图(指南针视图)编写应用程序,它工作得很好。我创建了自定义指南针视图的新实例,并使用了setcontent(mView)。

现在我试图使用布局,我只是不能让它工作!每次我尝试,应用程序崩溃。

这是代码:

自定义视图:

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();           
  }
}

xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout 
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<org.example.compassTest.CustomDrawableView
    android:id="@+id/compassview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>

主要活动:

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 && 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();
}
}

* * 日志:

    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

1 个答案:

答案 0 :(得分:4)

我认为你错过了这个构造函数:

public CustomDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

您正在尝试从布局xml传递参数构建视图,但您没有任何可以接收它们的构造函数。