Dinash
Dinash

Reputation: 3047

Android Custom View Problem

Hi Guys i have been fighting with this problem and don't know what is the reason. I have a simple layout as follows <?xml version="1.0" encoding="utf-8"?> <view xmlns:android="http://schemas.android.com/apk/res/android" class="com.dinash.notepad.NotePade$LinedEditText" />

and the activity class is as follows:

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);
    }
}

}

when i launch the application i keep on getting this error05-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

and following that

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

Could any body tel what the problem is... Thanks in advance

Upvotes: 0

Views: 1144

Answers (3)

Sujit
Sujit

Reputation: 10622

I think write your custom view in a separate class and your layout should be like this...

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

Upvotes: 1

ernazm
ernazm

Reputation: 9258

Move your custom view into separate file.

Upvotes: 1

Delyan
Delyan

Reputation: 8911

You need to make your inner class static. Otherwise, it would always require an instance of NotePade to give it an initialization context.

See here.

Upvotes: 4

Related Questions