2014-01-20 71 views
7

當該應用程序啓動時(公共布爾onCreateOptionsMenu(菜單菜單)),該代碼在logcat中失敗。任何人都可以請指出爲什麼如此?意外的文檔結尾(在android.view.MenuInflater.parseMenu)

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.official, menu); 
    official_menu = menu.findItem(R.id.ac_official); 
    important_menu = menu.findItem(R.id.ac_important); 
    normal_menu = menu.findItem(R.id.ac_normal); 
    create_menu = menu.findItem(R.id.ac_create); 
    if(dp.getDesignation().equalsIgnoreCase("student")){ 
     create_menu.setVisible(false); 
    } 
    profile_menu = menu.findItem(R.id.ac_profile); 
    return true; 
} 

logcat的

01-20 06:59:19.684: E/AndroidRuntime(996): FATAL EXCEPTION: main 
01-20 06:59:19.684: E/AndroidRuntime(996): java.lang.RuntimeException: Unexpected end of document 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.view.MenuInflater.parseMenu(MenuInflater.java:197) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.view.MenuInflater.inflate(MenuInflater.java:110) 
01-20 06:59:19.684: E/AndroidRuntime(996): at com.notice.official.OfficialActivity.onCreateOptionsMenu(OfficialActivity.java:183) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.app.Activity.onCreatePanelMenu(Activity.java:2504) 
01-20 06:59:19.684: E/AndroidRuntime(996): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:413) 
01-20 06:59:19.684: E/AndroidRuntime(996): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:775) 
01-20 06:59:19.684: E/AndroidRuntime(996): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:198) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.view.Choreographer.doFrame(Choreographer.java:531) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.os.Handler.handleCallback(Handler.java:730) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.os.Looper.loop(Looper.java:137) 
01-20 06:59:19.684: E/AndroidRuntime(996): at android.app.ActivityThread.main(ActivityThread.java:5103) 
01-20 06:59:19.684: E/AndroidRuntime(996): at java.lang.reflect.Method.invokeNative(Native Method) 
01-20 06:59:19.684: E/AndroidRuntime(996): at java.lang.reflect.Method.invoke(Method.java:525) 
01-20 06:59:19.684: E/AndroidRuntime(996): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
01-20 06:59:19.684: E/AndroidRuntime(996): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
01-20 06:59:19.684: E/AndroidRuntime(996): at dalvik.system.NativeStart.main(Native Method) 
+1

的例外似乎是在'com.notice.official.OfficialActivity.onCreateOptionsMenu(OfficialActivity.java:183)'。你可能試圖從'R.menu'中誇大畸形的XML文件嗎? – Nachi

+0

畸形?如在? –

+0

'意外的文檔結束'意味着XML無效。也許有些標籤沒有正確關閉? – Nachi

回答

3

我有同樣的問題,並在XML,是把一個子菜單<item>外面已經關閉/> 例子:

<item 
    android:id="@+id/menu_map" 
    android:title="Map" /> 

<item 
    android:id="@+id/menu_options" 
    android:title="Options" /> 

    <menu> 
     <item 
      android:id="@+id/menu_sync" 
      android:title="Sync" /> 
    </menu> 

只記得關閉子菜單後的<item>。 很簡單,但有時會發生。

0

膨脹菜單時意外的文檔異常結束意味着菜單(R.menu.official)沒有適當的標籤。你可以從這個網站查詢您的XML:http://www.xmlvalidation.com/

菜單樣本文件:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

<item 
    android:id="@+id/reset_count_menu" 
    android:orderInCategory="101" 
    android:showAsAction="never|collapseActionView" 
    android:title="@string/reset_count"/> 
<item 
    android:id="@+id/count_type_menu" 
    android:orderInCategory="102" 
    android:showAsAction="never|collapseActionView" 
    android:title="@string/count_forward_backward"/> 
<item 
    android:id="@+id/set_initial_count_menu" 
    android:orderInCategory="103" 
    android:showAsAction="never|collapseActionView" 
    android:title="@string/set_initial_count"/> 
<item 
    android:id="@+id/set_counter_name_menu" 
    android:orderInCategory="103" 
    android:showAsAction="never|collapseActionView" 
    android:title="@string/set_counter_name"/> 

</menu> 
相關問題