Custom view constructor does not get called on android 5.0.2-ThrowExceptions

Exception or error:

I created a custom view:

public class SomeView extends View

The custom view constructors:

public SomeView (Context context)
{
    super(context);
}
// Called when view is inflated from xml
public SomeView (Context context, AttributeSet attrs)
{
    super(context, attrs);
}
// Perform inflation from XML and apply a class-specific base style from a theme attribute.
public SomeView (Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

I also tried the 4th constructor from api 21 with no luck:

public VeediView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
    super(context, attrs,defStyleAttr, defStyleRes);
}

In the xml layout i am defining this view and things work fine.

Testing on Galaxy S2 works fine and the view constructor are called but when running the app on Nexus-7 android 5.0.2 the constructors do not get called at all.

Any idea why?

Could it be related to rooted devices?

The related xml view:

<com.package.name

        android:id="@+id/scene"
        android:onClick="startx"
        style="@style/txt_money_style"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:background="@drawable/wtbtn"
        android:layout_gravity="right"
        android:gravity="center_vertical|right"
        />
How to solve:

I think you should use this constructor for bestway:

public SomeView (Context context)
{
    this(context , null);
}
// Called when view is inflated from xml
public SomeView (Context context, AttributeSet attrs)
{
    this(context, attrs , 0);
}
// Perform inflation from XML and apply a class-specific base style from a theme attribute.
public SomeView (Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    // Initialize customize constructor here
}

###

In API 21 theres now a 4th constructor it could be that your XML is calling this.

From the docs:

public View (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Added in API level 21

Perform inflation from XML and apply a class-specific base style from a theme attribute or style resource. This constructor of View allows subclasses to use their own base style when they are inflating.

When determining the final value of a particular attribute, there are four inputs that come into play:

  1. Any attribute values in the given AttributeSet.
  2. The style resource specified in the AttributeSet (named “style”).
  3. The default style specified by defStyleAttr.
  4. The default style specified by defStyleRes.
  5. The base values in this theme.

Each of these inputs is considered in-order, with the first listed taking precedence over the following ones. In other words, if in the AttributeSet you have supplied , then the button’s text will always be black, regardless of what is specified in any of the styles.

Parameters

context The Context the view is running in, through which it can access the current theme, resources, etc.
attrs The attributes of the XML tag that is inflating the view.
defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.
defStyleRes A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.

###

Here is source code of referred View.java class. If you check it out you will see, that public View(Context context) is always called. If you think it’s not called but you see the view, then the issue is rather in the part detecting whether it gets called, than in Android code. You should look in there. It could be logging code or some wrong filters in AS, or similar.

From the source code you can also see, that this is the new constructor, used in Android 5.0 an higher, which has the most implementation.

public View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

###

The thing is i got this code and didnt develop it myself and after trying everything it turns out that the app have multiple layout files:

layout-large, layout-small etc…

I only defined the custom view on the layout folder so switching to other screen sizes invoked the regular view.

I guess others can learn from my mistake , i wish Android Studio or Eclipse can support some kind of setContentView(R.layout.activity_scene) and the related file debug option

So the answer is to make sure all layouts have the custom view defined

Leave a Reply

Your email address will not be published. Required fields are marked *