13

I have the following code, trying to create a simple ListAdapter in a ListView (I had this code working before and this is the only place I have changed something):

public BuddyListAdapter(Context context, HashMap<String, Buddy> hashMap) {
    buddyList = new ArrayList<Buddy>(hashMap.values());
    mInflater = LayoutInflater.from(context);
    ctx = context;
}

When stepping through, nothing happens, but when running, I get a NullPointerException in ChoreoGrapher.doCallbacks, when I create an empty ArrayList, it doesn't crash:

public BuddyListAdapter(Context context, HashMap<String, Buddy> hashMap) {
    buddyList = new ArrayList<Buddy>();
    mInflater = LayoutInflater.from(context);
    ctx = context;
}

What is the ChoreoGrapher and why is it crashing my app?

I am completely stuck on what could be the problem, or how to figure out what is wrong. The Eclipse debugger or crash information is not really helping.

The entire stack trace is as follows:

Thread [<1> main] (Suspended (exception NullPointerException))  
    Choreographer.doCallbacks(int, long) line: 558  
    Choreographer.doFrame(long, int) line: 525  
    Choreographer$FrameDisplayEventReceiver.run() line: 711 
    Handler.handleCallback(Message) line: 615   
    Choreographer$FrameHandler(Handler).dispatchMessage(Message) line: 92   
    Looper.loop() line: 137 
    ActivityThread.main(String[]) line: 4745    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 511  
    ZygoteInit$MethodAndArgsCaller.run() line: 786  
    ZygoteInit.main(String[]) line: 553 
    NativeStart.main(String[]) line: not available [native method]  

I also have a Handler in the main activity. When it is not doing anything, the listview doesn't crash. However, I remove all callbacks from it before openning the listview.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • As far as what Choreographer is, have a look at this: http://stackoverflow.com/questions/11266535/what-is-android-choreographer – manavo Sep 21 '12 at 14:59
  • Can you place breakpoints in your `Handler` instances to ensure they're not being called? – Craig Otis Sep 21 '12 at 15:02
  • @craig the `Handler` gets called every 5 seconds, when I open the `ListViewActivity` right after it has fired once, the app crashes immediately. – Bart Friederichs Sep 21 '12 at 15:06
  • It doesn't crash when the `ArrayList` is created empty. – Bart Friederichs Sep 21 '12 at 15:17
  • The `hashMap` comes from a app-wide available `HashMap`. When I 'manually' create the `buddyList`, it works. Apparently something goes wrong trying to use that `HashMap` in a ListView. – Bart Friederichs Sep 21 '12 at 15:28
  • 3
    Allow the crash to proceed in Eclipse, then use LogCat to look at the later stanzas ("Caused by") of the stack trace to find out the root cause. – CommonsWare Sep 21 '12 at 16:16
  • Because of a looming deadline, I decided to re-create the list as an `ArrayList` in the `ListViewAdapter`. In the future, I might re-open the issue to fix it better. – Bart Friederichs Sep 24 '12 at 13:56
  • I suddenly have this bug, only in Android 4.4. My app ran fine, now it crashes. Very hard to find out what is going on...! – Peterdk Nov 13 '13 at 21:16
  • 1
    @Peterdk, as CommonsWare says, let the crash go on and see the stacktrace in LogCat. It will give you more info (look down the trace). – Bart Friederichs Nov 14 '13 at 08:25

3 Answers3

30

I have this bug in my app only on Android 4.4.

In the debugger it shows the same message as the question asker. However when not debugging the full stacktrace in DDMS is:

Full stacktrace:

11-13 16:34:53.088: E/AndroidRuntime(1964): FATAL EXCEPTION: main
11-13 16:34:53.088: E/AndroidRuntime(1964): Process: appname, PID: 1964
11-13 16:34:53.088: E/AndroidRuntime(1964): java.lang.NullPointerException
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.TextView.makeNewLayout(TextView.java:6124)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.TextView.onMeasure(TextView.java:6419)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.Spinner.setUpChild(Spinner.java:632)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.Spinner.makeView(Spinner.java:585)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.Spinner.getBaseline(Spinner.java:431)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1262)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.View.measure(View.java:16458)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.Choreographer.doFrame(Choreographer.java:532)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.os.Handler.handleCallback(Handler.java:733)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.os.Looper.loop(Looper.java:137)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at java.lang.reflect.Method.invoke(Method.java:515)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-13 16:34:53.088: E/AndroidRuntime(1964):     at dalvik.system.NativeStart.main(Native Method)

Looking through the 4.4 sourcecode of TextView at line 6124, it turns out that this is caused by missing layout params when ellipsing. This is strange, since the code is failing inside a ListAdapter, where you don't supply layout params in getView.

I used to inflate android.R.layout.simple_spinner_item for standard spinner items. It looks like this item uses ellipsing. So in my getView I now call:

view.setEllipsize(null);

This fixes my problems on Android 4.4.

Edit

It turns out the problem is not the ellipsing, but improper inflating of a view in getView.

inflater.inflate(android.R.layout.simple_spinner_item, null);//WRONG
inflater.inflate(android.R.layout.simple_spinner_item, parent, false);//GOOD

Note that you supply false to not attach the view immediately to the parent, because this is in a adapter that attaches children when needed. Otherwise you will get errors.

When you inflate properly, you don't need to set ellipseSize to null. This is because when you inflate the good way, with a parent, it handles the LayoutParams properly, causing the ellipse code not to raise NullPointerException.

So, inflate properly, and then you don't have to worry anymore.

Peterdk
  • 15,625
  • 20
  • 101
  • 140
7

I am having the same thing again in another application. It seems to be caused by an unhandled exception in getView in the list adapter.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

Check for places where you may have invalid type casts when manipulating your views in the code. I had the exact same error with this line

picture.setLayoutParams(new ViewGroup.LayoutParams(50, 50));

The Choreographer was throwing this error "android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams"

changing it to

picture.setLayoutParams(new android.widget.LinearLayout.LayoutParams(50, 50));

to fix the problem.

Harshad
  • 1,344
  • 1
  • 10
  • 25
FER
  • 1