When I try to add a DatePicker to my layout, whenever the activity is launched, it crashes with a stack overflow.
This is the layout that crashes when displayed on the emulator (renders fine in eclipse):
</LinearLayout>
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:stretchColumns="1"
android:layout_below="@+id/topLayout" android:paddingBottom="5dip"
android:layout_marginBottom="50dip">
<TableRow android:layout_weight="1">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1">
<TextView android:layout_width="wrap_content"
android:text="Date:" android:layout_height="fill_parent"
android:gravity="center_vertical"></TextView>
</LinearLayout>
</TableRow>
<TableRow android:layout_weight="1">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1">
<DatePicker android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/datePicker" android:duplicateParentState="true"></DatePicker>
</LinearLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
<LinearLayout android:gravity="bottom|right"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_below="@+id/RelativeLayout01">
<Button android:layout_width="wrap_content" android:gravity="center"
android:layout_gravity="left" android:layout_height="fill_parent"
android:id="@+id/btnCancel" android:layout_weight="1" android:text="Cancel" />
<Button android:layout_width="wrap_content"
android:layout_gravity="right" android:gravity="center"
android:layout_weight="1" android:layout_height="fill_parent"
android:id="@+id/btnFinish" android:text="Finish" />
</LinearLayout>
</RelativeLayout>
And it crashes with this stack trace:
Thread [<3> main] (Suspended (exception StackOverflowError))
ViewRoot.draw(boolean) line: 1235
ViewRoot.performTraversals() line: 1030
ViewRoot.handleMessage(Message) line: 1482
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3948
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 782
ZygoteInit.main(String[]) line: 540
NativeStart.main(String[]) line: not available [native method]
When I comment out the DatePicker, it doesn’t crash.
How can I actually get the DatePicker to not crash this app?
Start by getting rid of the superfluous
LinearLayoutparent of theDatePicker.If that helps, or if the stack trace for your exception appeared to be in Android layout rendering code (e.g., calls to
layout()andonMeasure()and such), you probably have too complex of a UI. Use Hierarchy Viewer to examine your activity — if the longest path from the root container to the farthest leaf is much over 10, your UI is too complex. Simplify the user interface (e.g., switch from nestedLinearLayoutstoRelativeLayout, replace activities-in-tabs with views-in-tabs).