2011-05-26 108 views
0

嗨,大家好我一直在解決這個問題,不知道是什麼原因。我有一個簡單的佈局如下 <?xml version="1.0" encoding="utf-8"?> <view xmlns:android="http://schemas.android.com/apk/res/android" class="com.dinash.notepad.NotePade$LinedEditText" /> Android自定義查看問題

和活動類如下:

public class NotePade extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

public class LinedEditText extends EditText { 
    private Rect mRect; 
    private Paint mPaint; 
    // we need this constructor for LayoutInflater 
    public LinedEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mRect = new Rect(); 
     mPaint = new Paint(); 
     mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
     // mPaint.setColor(R.color.edit_note_line); //SET YOUR OWN COLOR 
     // HERE 
     mPaint.setColor(Color.RED); // SET YOUR OWN COLOR HERE 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // int count = getLineCount(); 
     int height = getHeight(); 
     // int height = 100; 
     int line_height = getLineHeight(); 
     int count = height/line_height; 
     if (getLineCount() > count) 
      count = getLineCount();// for long text with scrolling 

     Rect r = mRect; 
     Paint paint = mPaint; 
     int baseline = getLineBounds(0, r);// first line 
     for (int i = 0; i < count; i++) { 
      canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, 
        paint); 
      baseline += getLineHeight();// next line 
     } 
     super.onDraw(canvas); 
    } 
} 

}

當我啓動我不斷收到此錯誤應用05-26 11:31:21.604: ERROR/AndroidRuntime(1537): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.dinash.notepad.NotePade$LinedEditText

和之後

05-26 11:31:21.604: ERROR/AndroidRuntime(1537): Caused by: java.lang.NoSuchMethodException: LinedEditText(Context,AttributeSet)

可以在任何身體電話是什麼問題... 在此先感謝

回答

4

你需要讓你的內部類是靜態的。否則,它總是要求NotePade的一個實例給它一個初始化上下文。

請參閱here

+0

謝謝Delyan .... – Dinash 2011-05-26 12:04:45

1

將您的自定義視圖移動到單獨的文件中。

1

我覺得寫一個單獨的類自定義視圖和佈局應該是這樣的......

<com.dinash.notepad.LinedEditText></com.dinash.notepad.LinedEditText>