question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Paris] No default style for view leads to IllegalStateException

See original GitHub issue

Hi,

this is more of a question, as the causes that lead to the exception are well documented here under the “Recycling Views” paragraph.

In the last few months we’ve been looking for an easy way to set styles/attrs to our views/models and the Paris integration just feels like a godsend!

There’s something we’re missing though.

Our typical view (annotated with @ ModelView) is a custom view which usually extends either ConstraintLayout or LinearLayout. More importantly, we never apply styles from styles.xml to it, so we don’t really have a “default” one. We just want to set different paddings each time.

The documentation says:

By default the view’s default style is used, per Paris’s default rules.

If no default style is provided, Paris is using a DEFAULT_PARIS_STYLE, which is basically an empty style generated this way:

private static final Style DEFAULT_PARIS_STYLE = new MyViewStyleApplier.StyleBuilder().addDefault().build();

The following code leads to an IllegalStateException thrown by the assertSameAttributes() method.

myRecyclerView.withModels {
        myView {
                id(MyView.TAG)
                text(headerText)
                styleBuilder {   builder ->
                        builder.paddingBottomDp(10)
                }
        }
}
java.lang.IllegalStateException: MyViewModel_ model at position 0 has an invalid style:
The MyView style "EmptyStyle" is missing the following attributes:
✕ paddingBottom
It must declare the same attributes as the following styles:
a_programmatic_StyleBuilder

On the other hand, if I set a @ Style in MyView with option (isDefault = true) and then set paddings using styleBuilder, then everything works flawlessly.

So, my question is: is there any way we can just build the style using styleBuilder and avoid linking a default @ Style inside the view?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
reavcncommented, May 21, 2018

Thanks a lot !!! You’ve been very helpful and crystal-clear. Of course my intent was not to say that your approach was incorrect, but rather to understand whether I fully got the idea behind the recycling breaks.

@elihart Sure, I’ll be checking out https://github.com/airbnb/paris/pull/26 for updates! For now, I’ll define a @ Style(isDefault = true) with all the needed attributes and I’ll override values for them if I need to, similarly to what you said.

I also agree with @ngsilverman that views shouldn’t automatically inherit styles and I like the proposals, especially the 3rd one (“per style basis”).

Many thanks again, I really appreciate your support

2reactions
ngsilvermancommented, May 18, 2018

I still don’t think that views should automatically inherit linked styles from their parents because styles might not be designed for inheritance. We could make it possible to enable that behavior though:

// Inherits styles from its direct parent
@Styleable(inheritStyles = true)
public class MyView ...

Or the other way around:

// All subclasses will inherit styles from this class
@Styleable(openStyles = true)
public class MyBaseView ...

This could also be done on a per style basis:

@Style(open = true)
val defaultStyle = ...

And/or we could create this shortcut:

@Style
val defaultStyle = myViewStyle { addParentDefault() }

I like the openStyles/open option best because it’s the minimal code change: set it on the base class and you’re set. It also lets view types decide for themselves whether their styles are meant to be inherited.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom view default style not wokring without style tag
I have 2 custom view in my activity first without style tag and second with style tag but cardView style only apply when...
Read more >
DateTimeFormatter - Android Developers
By default, a formatter has no override chronology, returning null. See withChronology(java.time.chrono.Chronology) for more details on overriding.
Read more >
Web on Servlet Stack - Spring
If a model is returned, the view is rendered. If no model is returned (maybe due to a preprocessor or postprocessor intercepting the...
Read more >
Rome security and notable fixes - ServiceNow Docs
Click to open the incident record from the list view again. It changes to the default view instead of the ESS view. Service...
Read more >
DateTimeFormatterBuilder (ThreeTen date and time API)
Changes the parse style to be strict for the remainder of the formatter. Parsing can be strict or lenient - by default its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found