我找到了我在LinearLayout cannot be cast to 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位置的menuitem?是不是通过id来获取引用?
此外,即使使用第一个xml文件并启动应用程序,也不会显示该复选框的小方框。有趣的是,如果将具有id位置的menuitem的属性更改为 android:showAsAction =“always | withText”,则会出现小框复选框,但不带< / strong>文字。
我无法解决这个问题。请帮忙..
[更新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>
答案 0 :(得分: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。
答案 1 :(得分:0)
您的应用无法使用任何XML,因为未在任何这些XML中定义R.id.external
。请确保这是文件之间的唯一区别。