2014-09-25 69 views
0

我想在畫布上畫一些圖片,線條,圓環等,但我只想在小面積上,在部分屏幕上。我有這樣的佈局 「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 

我做錯了什麼?

+0

你是否在你的'activity'中初始化了'Draw2d'? – Rustam 2014-09-25 18:27:22

+0

不,這是應用程序中的所有代碼。我必須初始化'Draw2d' – missing17 2014-09-25 18:33:19

回答

3

看起來像是拼寫錯誤的packageName。檢查你的AndroidManifest。

com.example.canavstest.Draw2d

大概應該是

com.example.canvastest.Draw2d

通知canvas

編輯的拼寫:此外,你還沒有實現的LayoutInflater調用構造函數。添加這個。

public Draw2d(Context context, AttributeSet attrs){ 
    super(context, attrs); 
} 
+0

不,我看到我的錯誤標題))),但不,我複製這個packageName並把代碼 – missing17 2014-09-25 18:38:59

+0

我已經更新了我的答案。 – Simon 2014-09-25 18:43:20

+0

好的,是的。我更正了我的代碼,就像你的答案。和我的應用程序開始工作,謝謝 – missing17 2014-09-25 18:47:37