4

The exception:

>         10-13 11:47:32.151: E/AndroidRuntime(618): FATAL EXCEPTION: main
> 
>     10-13 11:47:32.151: E/AndroidRuntime(618): android.view.InflateException: Binary XML file line #3: Error
> inflating class com.example.demo.ToDoListView 10-13 11:47:32.151:
> E/AndroidRuntime(618):    at
> android.view.LayoutInflater.createView(LayoutInflater.java:596) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
> 10-13 11:47:32.151: E/AndroidRuntime(618):    at
> android.view.LayoutInflater.inflate(LayoutInflater.java:466) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.view.LayoutInflater.inflate(LayoutInflater.java:396) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
> 10-13 11:47:32.151: E/AndroidRuntime(618):    at
> android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.widget.AbsListView.obtainView(AbsListView.java:2267) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
> 10-13 11:47:32.151: E/AndroidRuntime(618):    at
> android.widget.ListView.onMeasure(ListView.java:1156) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.view.View.measure(View.java:15172) 10-13 11:47:32.151:
> E/AndroidRuntime(618):    at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
> 10-13 11:47:32.151: E/AndroidRuntime(618):    at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.view.View.measure(View.java:15172) 10-13 11:47:32.151:
> E/AndroidRuntime(618):    at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
> 10-13 11:47:32.151: E/AndroidRuntime(618):    at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.view.View.measure(View.java:15172) 10-13 11:47:32.151:
> E/AndroidRuntime(618):    at
> android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
> 10-13 11:47:32.151: E/AndroidRuntime(618):    at
> android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.view.View.measure(View.java:15172) 10-13 11:47:32.151:
> E/AndroidRuntime(618):    at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
> 10-13 11:47:32.151: E/AndroidRuntime(618):    at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-13
> 11:47:32.151: E/AndroidRuntime(618):  at
> android.view.View.measure(View.java:15172) 10-13 11:47:32.151:
> E/AndroidRuntime(618):    at
> android.widget.LinearLayout.measureVertical(LinearLayout.java:833)

This is my xml. It show no errors.

<?xml version="1.0" encoding="utf-8"?>

<com.example.demo.ToDoListView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="10dp"
  android:scrollbars="vertical"
  android:textColor="@color/notepad_text"
  android:fadingEdge="vertical"
/> 

I am trying to use it with a list adapter:

        aa=new ArrayAdapter<String>(this, R.layout.todolist_item,todoItems);

To Do list class:

public class ToDoListView extends TextView{
    private Paint marginPaint;
    private Paint linePaint;
    private int paperColor;
    private float margin;

    public ToDoListView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ToDoListView(Context context, AttributeSet st, int ds) {
        super(context);
        init();
    }

    private void init() {
      Resources myResources=getResources();
      marginPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
      marginPaint.setColor(myResources.getColor(R.color.notepad_margin));
      linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
      linePaint.setColor(myResources.getColor(R.color.notepad_lines));
      paperColor=myResources.getColor(R.color.notepad_paper);
      margin=myResources.getDimension(R.dimen.notepad_margin);      
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(paperColor);
        canvas.drawLine(0, 0, 0, getMeasuredHeight(), linePaint);
        canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);
        canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
        canvas.save();
        canvas.translate(margin, 0);
        super.onDraw(canvas);
        canvas.restore();
    }



}
4
  • 1
    I think "com.example.demo.ToDoListView" is not exact as it is in your custom class. Take you Package name from your AndroidManifest and class name from your java file. Commented Oct 13, 2012 at 11:53
  • Below that inflate exception should be the real cause of why it's happening Commented Oct 13, 2012 at 11:56
  • Chintant.. it didnt help.. see update Commented Oct 13, 2012 at 12:06
  • Post the code for the ToDoListView class. Commented Oct 13, 2012 at 12:12

2 Answers 2

16

If you use the custom view in the xml layout that you need to modify this constructor:

public ToDoListView(Context context, AttributeSet st, int ds) {
    super(context);
    init();
}

to:

public ToDoListView(Context context, AttributeSet st) {
    super(context, st);
    init();
}
Sign up to request clarification or add additional context in comments.

2 Comments

@DmitryMakovetskiyd Yes, otherwise the LayoutInflater will not find the constructor it needs to instantiate your view.
damn, it still throws that mistake
2

There is a nice pattern I use for custom views which covers all scenarios. Luksprog is of course correct but only covers one possible constructor. For default views:

public MyCustomView(Context context){
   this(context, null);
}

public MyCustomView(Context context, AttributeSet attrs){
   this(context, attrs, 0);
}

public MyCustomView(Context context, AttributeSet attrs, int defaultStyle){
   super(context, attrs, defaultStyle);
   init();
}

Using this pattern, no matter which overload is used, super and init are always correctly called. Your custom view will also render correctly in the UI previews assuming that onDraw can draw without external dependencies which are not resolvable pre-compile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.