4

I'm editing an open source project and find that a -1dp value for layout_marginTop is causing the graphical preview in Eclipse to fail with the error. The full error message, which probably includes an additional error, is:

java.lang.NullPointerException
"-1dp" in attribute "layout_marginTop" is not a valid format.
Exception details are logged in Window > Show View > Error LogThe graphics preview in the layout editor may not be accurate:
Paint.setShadowLayer is not supported. (Ignore for this session)

Do you know what the problem is?

Brian
  • 1,351
  • 2
  • 15
  • 29
  • 2
    Because negative values are not allowed? Since when could you draw a negative margin around an object. – IAmGroot Jul 11 '12 at 19:44
  • even if they where allowed, the Layout that needs negative values is basically just done wrong... – WarrenFaith Jul 11 '12 at 19:45
  • I'd start by taking out your negative value. – Addison Jul 11 '12 at 19:46
  • 1
    Actually, negative margins are allowable for LinearLayout and RelativeLayout according to @romainguy (http://stackoverflow.com/a/5293636/321697) – Kevin Coppock Jul 11 '12 at 19:47
  • @kcoppock. If that is true, it is likely not the case with the object he is trying to assign it to. I.e. not a linear or relative layout – IAmGroot Jul 11 '12 at 19:49
  • Right, but just clarifying that it is possible in some situations. (Romain Guy is one of the developers of Android so I'd take his word on it) – Kevin Coppock Jul 11 '12 at 19:51

2 Answers2

2

Quite simply put:

You cannot have a negative margin in your given situation

Due to this, your graphical preview does not know how to render it.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
2

The reason is because in previous versions of the Android SDK negative margins were permitted (up to 2.2?). Negative margins previously elicited an "unspecified behavior" aka they actually moved the layout in the negative direction. I've used the functionality before and it's actually quite useful at times.

My guess is that (1) the layout is up against the edge of the screen and that's what causes the error or (2) negative margins are now longer allowed and they throw errors. I can't confirm that though.

I'd just change it to zero.

Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
  • 1
    I need to test when I get home, but I'm fairly sure I've used a negative margin on ICS. – Kevin Coppock Jul 11 '12 at 20:01
  • 2
    I haven't been able to test to confirm this answer. Approving just to reward the better answer. – Brian Jul 16 '12 at 02:24
  • Yes you can, certainly in ICS and I think JB as well. In fact negative margins work really well - I guess it will crash if you don't have room to handle it, but it's ideal for a RelativeLayout when you want to overlap something at the bottom of an image. I'm using it to recreate the list UI in the Google+ app for a similar social media platform app I'm working on, with the user's profile pic overlapping the bottom of the posted image. – BasicPleasureModel Nov 22 '12 at 13:41