I’m new to Android development and I’m trying to implement a custom view to act as a “custom menu button” for my app.
I followed the instructions at http://developer.android.com/training/custom-views/create-view.html but by the end of the implementation I get a message saying “Unfortunately customviews1 has stopped” and the app just shut’s down.
My approach is very simple and I cannot find any reference about solving this basic problem. Here’s what I’m doing:
-
create a new Android project in Eclipse named “customviews1”
-
I run the project and it shows a “Hello World” TextView on the “activity_main.xml” layout file
-
I add a new class named MyCustomView that extends “View” to the “src” folder of the project
public class MyCustomView extends View { public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); } } -
I remove the “TextView” tag from activity_main.xml and add a “customview1” to it:
<com.example.customviews1.MyCustomView android:id="@+id/myCustomView1" /> -
I run the app again and I got the message saying “Unfortunately customviews1 has stopped” and the app shuts down.
Is there any code I’m missing here?
Thanks for any clue,
Regards,
Victor Reboucas
if you check your LogCat output you will find that an error saying that you have to specify layout_width and layout_height in your layout.
so write:
and it should work.