Reputation: 2424
When I try to add a sub menu to my application it crashes. When I remove the sub menu it works fine.
menu_main.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_action_setting"
android:title="@string/action_settings"
android:orderInCategory="1"
app:showAsAction="ifRoom"
tools:ignore="AppCompatResource" />
<item
android:id="@+id/action_language"
android:icon="@mipmap/ic_launcher"
android:title="@string/langauge"
android:orderInCategory="2"
app:showAsAction="ifRoom"
tools:ignore="AppCompatResource" />
<item
android:id="@+id/action_language2"
android:icon="@drawable/ic_action_drawing"
android:title="language2"
android:orderInCategory="3"
app:showAsAction="ifRoom"
tools:ignore="AppCompatResource" />
<menu>
<item
android:id="@+id/action1"
android:title="item one">
</item>
<item
android:id="@+id/action2"
android:title="item two">
</item>
</menu>
</menu>
When I remove the above sub menu (with items "item one" and item "item two", the app runs fine. Here is the logcat output which crashes with the message java.lang.RuntimeException: Unexpected end of document What does it expect? can't I have a sub menu at the last item of the menu? Note that if I move the sub menu to the 2nd item of the main menu the app does not crash.
LogCat
07-30 05:09:02.890 16982-16982/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
07-30 05:09:03.000 16982-16997/? D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-30 05:09:03.060 16982-16982/? D/Atlas﹕ Validating map...
07-30 05:09:03.140 16982-16997/? D/﹕ HostConnection::get() New Host Connection established 0x7f023fd1bf40, tid 16997
07-30 05:09:03.160 16982-16997/? I/OpenGLRenderer﹕ Initialized EGL, version 1.4
07-30 05:09:03.200 16982-16997/? D/OpenGLRenderer﹕ Enabling debug mode 0
07-30 05:09:03.220 16982-16997/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-30 05:09:03.220 16982-16997/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f023ffd4fc0, error=EGL_SUCCESS
07-30 05:09:03.270 16982-16982/? D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
07-30 05:09:03.270 16982-16982/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: il.co.talkie.q, PID: 16982
java.lang.RuntimeException: Unexpected end of document
at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
at il.co.talkie.q.MainActivity.onCreateOptionsMenu(MainActivity.java:61)
at android.app.Activity.onCreatePanelMenu(Activity.java:2823)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:275)
at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1117)
at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1399)
at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:89)
at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:126)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Upvotes: 0
Views: 601
Reputation: 4643
You must put the sub-menu inside the wanted item, and close the item after the sub-menu.
This should be like this
<item
android:id="@+id/action_language2"
android:icon="@drawable/ic_action_drawing"
android:title="language2"
android:orderInCategory="3"
app:showAsAction="ifRoom"
tools:ignore="AppCompatResource" >
<menu>
<item
android:id="@+id/action1"
android:title="item one">
</item>
<item
android:id="@+id/action2"
android:title="item two">
</item>
</menu>
</item>
Upvotes: 2