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):
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.
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.
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.