我刚刚从 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"/>
日志猫:
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)
我使用了Android Studio自带的Blank Activity,只修改了main_activity_actions XML文件。我不知道我是不是出了什么问题,或者 Appcompat 库是否有错误。
如果你能帮助我,我将不胜感激。谢谢你。