Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app crashes with NullPointerException in ChoreoGrapher

Tags:

java

android

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.

like image 838
Bart Friederichs Avatar asked Sep 21 '12 14:09

Bart Friederichs


2 Answers

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.

like image 84
Peterdk Avatar answered Oct 24 '22 18:10

Peterdk


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.

like image 26
Bart Friederichs Avatar answered Oct 24 '22 19:10

Bart Friederichs