0
I’ve been trying for some time to put a Button on top of a View I’m using with Canvas. I looked in several places (this seems to be a common problem) but found no answer.
When I try to run the program below, it gives the error:
java.lang.ClassCastException: android.view.View cannot be cast to com.examples.danilofernandes.canvasonrelativelayout.RandomShapeView
Any idea how to do what I need?
Thank you very much
public class MyActivity extends Activity {
private RandomShapeView mDrawingArea;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mDrawingArea = (RandomShapeView) findViewById(R.id.drawing_area);
mDrawingArea.setBackgroundColor(Color.GREEN);
}
}
Other class
public class RandomShapeView extends View {
Paint paint;
public RandomShapeView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.BLACK);
}
@Override
protected void onDraw(Canvas canvas) {
}
}
Layout:
<RelativeLayout 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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<Button
android:id="@+id/button"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
class="com.examples.danilofernandes.canvasonrelativelayout.RandomShapeView"
android:id="@+id/drawing_area"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
Thanks, it worked. Just needed to implement a constructor, which is exactly what was said in this topic. http://stackoverflow.com/questions/8352249/error-in-creation-of-custom-view
– user2929914