Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.view.InflateException: Binary XML file line when using custom view

Tags:

android

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



}
like image 261
Dmitry Makovetskiyd Avatar asked Dec 07 '22 11:12

Dmitry Makovetskiyd


1 Answers

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();
}
like image 185
user Avatar answered Jan 20 '23 23:01

user