我为我在LinearLayout 上发布的一个早期问题得到了解决方案,该问题无法转换为 android.widget.checkbox。然而,一个新的问题出现了。action.xml 文件有:
<?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 位置的 menuitem 的属性更改为android:showAsAction="always|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);
如果这一行被注释掉,它运行良好。但我想获得对复选框的引用。
[更新 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>