here is my xml in which i am using my custom view . that view is in another package named com.soft.MyView and the class which is extending view is also named MyView now m getting force close and the error in logcat is 07-21 16:26:29.936: ERROR/AndroidRuntime(19854): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.soft.MyView.MyView ..plss tell me where am i wrong??
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout1" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/TextView1" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.soft.MyView.MyView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/TextView1" android:layout_centerHorizontal="true" android:layout_marginTop="187dp" android:id="@+id/drawingImageView"/>
</RelativeLayout>
and here is my class MyView
public class MyView extends View{
class Pt{
float x, y;
Pt(float _x, float _y){
x = _x;
y = _y;
}
}
Pt[] myPath = { new Pt(100, 100),
new Pt(200, 200),
new Pt(200, 500),
new Pt(400, 500),
new Pt(400, 200)
};
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
Path path = new Path();
path.moveTo(myPath[0].x, myPath[0].y);
for (int i = 1; i < myPath.length; i++){
path.lineTo(myPath[i].x, myPath[i].y);
}
canvas.drawPath(path, paint);
}
}
the reason for your problem is that you used only the code-version CTOR:
public View (Context context)
quote from android developers guide:
the solution would be to add all of the possible CTORs that the custom view needs for the layout inflating process, or at least this one:
public View (Context context, AttributeSet attrs)
quote from android developers guide: