2010-08-30 61 views
0

我正在嘗試創建最簡單的2D繪圖程序,以便確定我無法弄清楚繪圖的內容。這個程序應該從字面上畫一個20x20的矩形。下面是我有:幫助:簡單的繪圖視圖/程序

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <com.example.drawing 
    android:id="@+id/DrawView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 
</FrameLayout> 

drawView函數:

package com.example.drawing; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.view.View; 


public class DrawView extends View{ 


    Paint mPaint; 


    public DrawView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setFocusable(true); 
     initDrawView(); 
    } 

    public DrawView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setFocusable(true); 
     initDrawView(); 
    } 

    public void initDrawView(){ 
     mPaint = new Paint(); 
     mPaint.setColor(0xFF000000); 
    } 

    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.drawRect(0,0,20,20, mPaint); 
    } 





} 

活動:

package com.example.drawing; 

import android.app.Activity; 
import android.os.Bundle; 

public class HelloDraw extends Activity { 
    private DrawView dr; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

該程序將無法運行。我錯過了什麼?我相信這是荒謬的事情,但我似乎無法找到它。

+1

當你說它沒有運行...它至少會給你一個錯誤,或只是它沒有做任何事情? – FrustratedWithFormsDesigner 2010-08-30 20:51:31

+0

模擬器錯誤: 「應用程序Hello,Draw!(process com.example.drawing)已意外停止,請重試。」 線在調試的logcat: 「 致命異常:主 1月8日至31日:28:43.197:ERROR/AndroidRuntime(283):了java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.drawing/com.example.drawing.HelloDraw}:android.view.InflateException:二進制XML文件行#7:錯誤膨脹類com.example.drawing – 2010-08-31 01:30:35

回答

1

從您的例外情況來看,您期望佈局中標記名稱的com.example.drawing是您提供的類的名稱。您的完全合格課程名稱是com.example.drawing.DrawView