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