I am creating an app which requires a custom layout.But after running my app it simple force closes.Can anyone suggest the reason and solution if possible
Thanx in advance
this is my main Activity
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
this is my custom layout class
public class Odo extends View {
private Paint p;
private RectF rect;
public Odo(Context context) {
super(context);
Log.e("odo", "constructor");
init();
// TODO Auto-generated constructor stub
}
public void init() {
Log.e("odo", "inside init()");
rect = new RectF();
p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(Color.GREEN);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
Log.e("odo", "inside onSizeChange");
rect.set(0, 0, w, h);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Log.e("odo", "inside onDraw");
super.onDraw(canvas);
canvas.drawRect(rect, p);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.e("odo", "inside onMeasure");
}
}
And this is my XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.odo.Odo
android:id="@+id/odom"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
I believe you need to provide the default constructors that takes
AttributeSetand/orintfor style reference. For example: