Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TranslationLayout NullPointerException #42

Closed
tekblom opened this issue Oct 19, 2013 · 9 comments
Closed

TranslationLayout NullPointerException #42

tekblom opened this issue Oct 19, 2013 · 9 comments

Comments

@tekblom
Copy link

tekblom commented Oct 19, 2013

Hi,

I get an error that make my app crash. It says that it is a problem in the TranslationLayout. Right before the stacktrace that i add below I get this warning:

threadid=1: thread exiting with uncaught exception (group=0x40bed1f8)

Stacktrace:
dalvik.system.NativeStart ? in main at line -2
com.android.internal.os.ZygoteInit ? in main at line 745
com.android.internal.os.ZygoteInit$MethodAndArgsCaller ? in run at line 978
java.lang.reflect.Method ? in invoke at line 511
java.lang.reflect.Method ? in invokeNative at line -2
android.app.ActivityThread ? in main at line 4507
android.os.Looper ? in loop at line 137
android.os.Handler ? in dispatchMessage at line 99
android.view.ViewRootImpl ? in handleMessage at line 2649
android.view.ViewRootImpl ? in performTraversals at line 1255
android.view.View ? in measure at line 12922
com.android.internal.policy.impl.PhoneWindow$DecorView ? in onMeasure at line 2261
android.widget.FrameLayout ? in onMeasure at line 293
android.view.ViewGroup ? in measureChildWithMargins at line 4706
android.view.View ? in measure at line 12922
android.widget.LinearLayout ? in onMeasure at line 563
android.widget.LinearLayout ? in measureVertical at line 670
android.widget.LinearLayout ? in measureChildBeforeLayout at line 1385
android.view.ViewGroup ? in measureChildWithMargins at line 4706
android.view.View ? in measure at line 12922
android.widget.FrameLayout ? in onMeasure at line 293
android.view.ViewGroup ? in measureChildWithMargins at line 4706
android.view.View ? in measure at line 12922
android.widget.RelativeLayout ? in onMeasure at line 376
android.widget.RelativeLayout ? in measureChildHorizontal at line 594
android.view.View ? in measure at line 12922
android.widget.RelativeLayout ? in onMeasure at line 392
android.widget.RelativeLayout ? in measureChild at line 579
android.view.View ? in measure at line 12922
android.widget.RelativeLayout ? in onMeasure at line 376
android.widget.RelativeLayout ? in measureChildHorizontal at line 594
android.view.View ? in measure at line 12922
android.widget.RelativeLayout ? in onMeasure at line 376
android.widget.RelativeLayout ? in measureChildHorizontal at line 594
android.view.View ? in measure at line 12922
com.qozix.layouts.ZoomPanLayout ? in onMeasure at line 438
android.view.ViewGroup ? in measureChildren at line 4654
android.view.ViewGroup ? in measureChild at line 4677
android.view.View ? in measure at line 12922
com.qozix.layouts.StaticLayout ? in onMeasure at line 16
android.view.ViewGroup ? in measureChildren at line 4654
android.view.ViewGroup ? in measureChild at line 4677
android.view.View ? in measure at line 12922
com.qozix.layouts.TranslationLayout ? in onMeasure at line 46
android.view.ViewGroup ? in measureChildren at line 4653
Caused by: java.lang.NullPointerException ? at line -1

Do you have any ideas?

@tekblom
Copy link
Author

tekblom commented Oct 19, 2013

I can't get when it happends and it happends rarely. It seems to be randomly, but could happen when I open poups that not are on the map but is placed on top of it. These ones also get some data.

Sometimes it just crashes when I have it open. It feels like the views has been garbage collected or something. I have a lot of markers on my map, at least 100. And some objects that are moving on it.

@tekblom
Copy link
Author

tekblom commented Oct 19, 2013

Done some more digging.

I think the problem is when I remove a marker. My theory is that onMeasure in TranslationLayout is running and the view is removed while it is running measureChildren and then it have a discapency between mChildrenCount and the actual number of children (see http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/view/ViewGroup.java#ViewGroup.measureChildren%28int%2Cint%29).

Possible? I'm gonna look deeper into it tomorrow.

@moagrius
Copy link
Owner

Hm... Of course I can't tell without seeing the code and debugging myself, but I'd be surprised if that were the issue. Almost all overridden onLayout/onMeasure methods I've seen do the same basic loop. Should be easy to tell, though - just wrap it in a try/catch or add a null check:

for (int i = 0; i < count; i++) {
  View child = getChildAt(i);
  if (child == null) continue;
  ...

or

for (int i = 0; i < count; i++) {
  try {
    View child = getChildAt(i);
    ...

Or maybe test the count in the evaluation expression, so instead of:

int count = getChildCount();
for (int i = 0; i < count; i++) {

You could try

for (int i = 0; i < getChildCount(); i++) {

I'd be interested to hear what you discover.

@tekblom
Copy link
Author

tekblom commented Oct 20, 2013

Yeah, I feel the same way. It feels a bit strange that there could be a syncronization problem there. Just a note, it mostly happends in:

TranslationLayout.java - onMeasure:

measureChildren(widthMeasureSpec, heightMeasureSpec);

ViewGroup.java

  protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        final int size = mChildrenCount; // This one could be out of sync
        final View[] children = mChildren;
        for (int i = 0; i < size; ++i) {
            final View child = children[i]; // This probably returns null
            if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
    }

I'm gonna do some testing now!

@tekblom
Copy link
Author

tekblom commented Oct 20, 2013

I'm not any closer.

I catch the NPE with a try around all code in onMeasure but then it fails in onLayout :/ And the state of the NPE didn't give me that much.. But I'm gonna try to write out the index of the child that is null.

Unfortunately I can't reproduce the error so it is really hard debugging.

New day tomorrow... Any ideas?

And I'm thinking, I have 150+ children. Does it not take relatively long time to loop them and couldn't one get removed and cause that nullpointer.. ? Feels unlikeley but a possability..

Thanks.

@moagrius
Copy link
Owner

If wrapping onMeasure with try/catch pushed the NPE off to onLayout, it sounds like you're on the right track at least. I'd try those simple tricks above in both, and see what happens.

That said - notice that your stack trace shows the error on line 46 - line 46 is the call to measureChildren, which makes me think the NPE is not in the loop directly...

Regarding this:

And I'm thinking, I have 150+ children. Does it not take relatively long time to loop them and couldn't one get removed and cause that nullpointer.. ? Feels unlikeley but a possability..

I don't think so. I'm not 100% sure, but AFAIK, that could only happen if the two actions (onMeasure, and removal) happened in different threads.

To clarify: the NPE only happens when you remove markers?

@tekblom
Copy link
Author

tekblom commented Oct 20, 2013

I am actually not sure when it happends. It was my theory that it happened when I removed a marker.

I havn't yet found a way to trigger the error. That's the key. Will try removing add adding a lot tomorrow.

No, normally it is not direct in the loop. It is in the ViewGroup.java method measureChildren but if it passes that (for example with try catch) it causes the error in the loop.

I'm still confused how a child can be null...

@moagrius
Copy link
Owner

post back if i can help

@moagrius
Copy link
Owner

@tekblom did you get this worked out? or should i leave this issue open?

@moagrius moagrius closed this as completed Nov 4, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants