我想在畫布上畫一些圖片,線條,圓環等,但我只想在小面積上,在部分屏幕上。我有這樣的佈局 「activity_main」在畫布上畫上一部分的畫面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.example.canavstest.Draw2d
android:id="@+id/game"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_vertical|center_horizontal" />
和主類
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
和所在班級我畫
public class Draw2d extends View{
public Draw2d(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);
canvas.drawPaint(paint);
}
}
當我嘗試啓動我的應用程序獲得下一錯誤
09-25 21:06:43.925: E/AndroidRuntime(28370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.canavstest/com.example.canavstest.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class com.example.canavstest.Draw2d
我做錯了什麼?
你是否在你的'activity'中初始化了'Draw2d'? – Rustam 2014-09-25 18:27:22
不,這是應用程序中的所有代碼。我必須初始化'Draw2d' – missing17 2014-09-25 18:33:19