刚刚开始接触android。这是《android2.0开发宝典》上的一个实例程序,开始运行起来老是出错,DDMS里的错误信息是“Caused by:java.lang.RuntimeException: Binary Xml file line#7: you must supply a layout_width attribute”,求教达人们这个问题如何解决呐?
java文件:
package wyf.ytl;
import android.content.Context;//引入相关的类
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View{//继承自View
Bitmap myBitmap;//图片的引用
Paint paint;//画笔的引用
public MyView(Context context, AttributeSet attrs) {//构造器
super(context, attrs);
// TODO Auto-generated constructor stub
this.initBitmap();  
}
public void initBitmap(){  
paint = new Paint();//创建一个画笔
myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img);//获得图片资源
}
@Override
protected void onDraw(Canvas canvas) {//重写的绘制方法
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setAntiAlias(true);//打开抗锯齿
paint.setColor(Color.WHITE);//设置画笔的颜色
paint.setTextSize(15);
canvas.drawBitmap(myBitmap, 10, 10, paint);//绘制图片
canvas.drawText("图片的宽度: "+myBitmap.getWidth(), 20, 220, paint);//绘制字符串,图片的宽度
canvas.drawText("图片的高度: "+myBitmap.getHeight(), 150, 220, paint);//绘制字符串,图片的高度
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_height="fill_parent"  
  android:layout_width="fill_parent"
  >
  <wyf.ytl.MyView
  android:id="@+id/myView"
  android.layout_width="fill_parent"
  android.layout_height="fill_parent"
  />
</LinearLayout>