溢出菜单不适用于 Appcompat
Overflow Menu doesn't work on Appcompat
我刚刚从 Eclipse 转移到 Android Studio,我正在使用 Appcompat 库,因为它默认出现在 Android Studio 中并且很难停用它。
问题是,当我创建一个 ActiveBar 时,应该显示在溢出菜单中的项目从未出现,溢出菜单图标(3 个点)也没有出现。
只创建栏中的项目,如果我触摸打开溢出菜单的硬件键,应用程序崩溃。
MainActivity.java:
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.learning.app1.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
main_activity_actions:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never"/>
Logcat:
01-24 02:48:18.111 14717-14717/com.learning.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x412fa438)
01-24 02:48:18.131 14717-14717/com.learning.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:986)
at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1687)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2098)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3621)
at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3591)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2834)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4905)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
我用的是AndroidStudio自带的BlankActivity,只修改了main_activity_actionsXML文件。不知道是我哪里有问题还是Appcompat库有bug
如果你能帮助我,我将不胜感激。谢谢。
这是 LG 设备上的常见问题。看我评论里的link
我刚刚从 Eclipse 转移到 Android Studio,我正在使用 Appcompat 库,因为它默认出现在 Android Studio 中并且很难停用它。
问题是,当我创建一个 ActiveBar 时,应该显示在溢出菜单中的项目从未出现,溢出菜单图标(3 个点)也没有出现。
只创建栏中的项目,如果我触摸打开溢出菜单的硬件键,应用程序崩溃。
MainActivity.java:
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.learning.app1.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
main_activity_actions:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never"/>
Logcat:
01-24 02:48:18.111 14717-14717/com.learning.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x412fa438)
01-24 02:48:18.131 14717-14717/com.learning.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:986)
at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1687)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2098)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3621)
at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3591)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2834)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4905)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
我用的是AndroidStudio自带的BlankActivity,只修改了main_activity_actionsXML文件。不知道是我哪里有问题还是Appcompat库有bug
如果你能帮助我,我将不胜感激。谢谢。
这是 LG 设备上的常见问题。看我评论里的link