How to inserting a custom view in XML in Android

拥有回忆 提交于 2019-12-25 18:20:01

问题


I have a class A that extends class View.

I have a class B that extends class A.

Now I'm trying to add class B into my xml however I'm unable to do that whereas I'm able to add class A into my xml.

One more thing which I've noticed is that all other custom classes which directly extends View are visible inside my xml.

I wanna know whether is there any way of adding a class which extends another class which in turn extends View into my xml?

NOTE : I'm using proper xml format and complete package name.

CLASS A

public class A extends View implements Serializable{

    public A(final Context context, final AttributeSet attrs,
            final int defStyle, final SomeEnum myType) {
        super(context, attrs, defStyle);
    }

    public A(final Context context, final AttributeSet attrs,
            final SomeEnum myType) {
        this(context, attrs, 0, testStateType);
    }

    public A(final Context context,
            final SomeEnum myType) {
        this(context, null, 0, testStateType);
    }

    public A(final Context context,
            final AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(final int w, final int h, final int oldw,
            final int oldh) {
        invalidate();
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec,
            final int heightMeasureSpec) {
    }

    @Override
    protected void onDraw(Canvas canvas) {

    }
}

CLASS B

public class B extends A
{
    public B(final Context context,
            final AttributeSet attrs, final int defStyle, final SomeEnum myType)
    {
        super(context, attrs, defStyle, testStateType);
    }

    public B(final Context context, final AttributeSet attrs, final SomeEnum myType)
    {
        this(context, attrs, 0, testStateType);
    }

    public B(final Context context, final SomeEnum myType)
    {
        this(context, null, 0, testStateType);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
    }
}

回答1:


class B is missing the constructor with the default arguments. Add:

public B(final Context context, final AttributeSet attrs) {
    super(context, attrs);
}

to your class and you will be able to inflate it from an XML with your other views.

In general, adding custom parameters to a View's constructors is not a good practice. Use a custom attribute for that.




回答2:


Custom views must have the following constructors:

public CustomView(Context context)
{
    super(context);
}

public CustomView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public CustomView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

Looks like you only have overloads that receive a SomeEnum parameter, but the LayoutInflater doesn't know how to call those.




回答3:


Why you getting android inflating exception error ?

Mostly it is caused for xml error. If you are trying to set a properties of a view by illegal resources , layouts etc, your program will crash as your brain. For an example: You can not set a textView background by an layout xml file, you know that very well. But it may happens when you getting fast and using eclipse suggestions. Looking at logcat you can see ” Binary XML file line 20 or something” . Its telling you at which line the error is.

Caused by:

android.view.InflateException: Binary XML file line #9: Error inflating class

Solution:

This tells that the class name you defined is not found. Check the class name carefully or check the build path. If one of this resources has a high pixel resolution it would take a lot of memory causing then an inflate exception.

For more common android InflateExceptions: Android.view.InflateException: Binary XML file line # xx.



来源:https://stackoverflow.com/questions/24265081/how-to-inserting-a-custom-view-in-xml-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!