0

我得到了一個解決方案,我發佈在LinearLayout cannot be cast to android.widget.checkbox的早期問題。 然而,一個新問題突然出現。該action.xml文件有:NullPointerException在發佈

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

<item 
    android:id="@+id/location" 
    android:actionLayout="@layout/action_location" 
    android:showAsAction="never" 
    android:title="location"> 

</item> 
<item 
    android:id="@+id/save" 
    android:title="Save" 
    android:showAsAction="always"> 

</item> 
<item 
    android:id="@+id/saveBackground" 
    android:showAsAction="always|withText" 
    android:title="in BG"> 

</item> 

通過以上的XML文件,它工作正常。但是,如果我將其修改爲以下內容,則會在啓動時收到NullPointerException錯誤。

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


<item 
    android:id="@+id/save" 
    android:title="Save" 
    android:showAsAction="always"> 

</item> 
<item 
    android:id="@+id/saveBackground" 
    android:showAsAction="always|withText" 
    android:title="in BG"> 

</item> 
<item 
    android:id="@+id/location" 
    android:actionLayout="@layout/action_location" 
    android:showAsAction="never" 
    android:title="location"> 

</item> 


</menu> 

如何與菜單之內ID位置的菜單項的位置?是不是通過id獲得了參考?

此外,即使使用第一個xml文件並啓動應用程序,複選框的小框也不會出現。有趣的是,如果與ID位置菜單項的屬性更改爲機器人:showAsAction =「總是| withText」,爲複選框小盒子顯示,但不文本。

我還沒有能夠解決這個問題。請幫助..

[更新1]

下面是來自logcat的整個堆棧跟蹤:

07-24 09:41:49.539: E/AndroidRuntime(2354): FATAL EXCEPTION: main 
07-24 09:41:49.539: E/AndroidRuntime(2354): java.lang.NullPointerException 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at com.example.files_readwrite.FilesDemoActivity.onCreateOptionsMenu(FilesDemoActivity.java:29) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:44) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:65) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:165) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:411) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:784) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:205) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at android.view.Choreographer.doCallbacks(Choreographer.java:579) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at android.view.Choreographer.doFrame(Choreographer.java:547) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at android.os.Handler.handleCallback(Handler.java:725) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at android.os.Looper.loop(Looper.java:153) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at android.app.ActivityThread.main(ActivityThread.java:5297) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
07-24 09:41:49.539: E/AndroidRuntime(2354):  at dalvik.system.NativeStart.main(Native Method) 

[更新2] 線FilesDemoActivity.java 29具有

external=(CheckBox) menu.findItem(R.id.location).getActionView().findViewById(R.id.external); 

如果這條線被註釋掉,它運行良好。但我想獲得對複選框的引用。

[UPDATE 3]的action_location.xml含量:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<CheckBox 
    android:id="@+id/external" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:title="@+string/external" 
    /> 


</LinearLayout> 
+2

請從logcat發佈整個堆棧跟蹤。 – m0skit0

+0

@ m0skit0:我已經包含堆棧跟蹤,請檢查... – user2499998

+1

嘗試清理項目.. –

回答

0

您的應用不能與任一XML因爲R.id.external在任何那些個XML的沒有定義工作。請確保這是文件之間的唯一區別。

+0

'R.id.external'在'android:actionLayout =「@ layout/action_location'中定義,我添加了一個顯示xml文件內容的[UPDATE 3] – user2499998

+0

請參閱@KarelG回答 – m0skit0

+0

試過了,同樣的問題仍然堅持:( – user2499998

1

由於您正在嘗試查找對子佈局的引用,因此您必須定位正確的XML文件。 Android無法自行完成。

編輯1,更新。開始一個項目,看看它是否會工作。現在它確實,見下文;

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.action, menu); 

    // inflate parent view  
    View parentView = null; 
    Log.d(null,"159 - foo --"); 
    // then inflate the xml, that it exists on android 
    try { 
     LayoutInflater inflater = LayoutInflater.from(this.getBaseContext()); 
     parentView = inflater.inflate(R.layout.action_location, null); 
    } 
    catch (Exception e) { 
     Log.d(null, "159 - error after inflater : " + e.getMessage()); 
    } 
    Log.d(null,"159 - bar --"); 
    View childView = parentView.findViewById(R.id.external); 
    Log.d(null,"159 - success --"); 
    return true; 
} 

結果是foo + bar +成功登錄。所以它工作。我必須用LayoutInflater調整它,因爲Android在運行時沒有包含action_location.xml。

+0

不行,同樣的問題仍然存在,M不知道這是否重要,但基類是SherlockFragmentActivity(只是爲了防止有所差別) – user2499998

+0

我希望它現在可以爲你工作。 – KarelG

相關問題