4

例外:

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

这是我的xml。它没有显示错误。

<?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"
/> 

我正在尝试将它与列表适配器一起使用:

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

待办事项列表类:

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

2 回答 2

15

如果在xml布局中使用自定义视图需要修改这个构造函数:

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

到:

public ToDoListView(Context context, AttributeSet st) {
    super(context, st);
    init();
}
于 2012-10-13T12:16:37.590 回答
1

我为自定义视图使用了一个很好的模式,它涵盖了所有场景。Luksprog 当然是正确的,但只涵盖了一种可能的构造函数。对于默认视图:

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

使用这种模式,无论使用哪种重载,super 和 init 总是被正确调用。您的自定义视图也将在 UI 预览中正确呈现,假设 onDraw 可以在没有外部依赖项的情况下进行绘制,这些依赖项是不可解析的预编译。

于 2012-10-13T12:55:57.597 回答