*已回答!:*事实证明,即使您有一个项目的图标,您也需要为该项目提供标题。
我目前正在我的应用程序中实现 (Sherlock)Action Bar。我目前有一个屏幕上的按钮来刷新我的屏幕视图,我想将此按钮移动为我的操作栏中的一个项目。onOptionsItemSelected 的日志未显示在 LogCat 中,但已创建菜单显示。
我不知道为什么我得到一个空指针,并且在错误日志中找不到空值发生的位置。该应用程序仅在我单击操作栏中的刷新按钮时才会崩溃,并且在此之前一切正常。
public boolean onCreateOptionsMenu(Menu menu) {
Log.i("Created Menu", "Yes");
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
Log.i("Got into onOptionsItemSelected", "Yes");
switch(item.getItemId()){
case R.id.action_refresh:
new getFleetList().execute(getFleetListURL);
progressBar.setVisibility(ProgressBar.VISIBLE);
return true;
}
堆栈跟踪:
07-09 12:52:27.594: W/dalvikvm(23570): threadid=1: thread exiting with uncaught exception (group=0x4132e930)
07-09 12:52:27.601: E/AndroidRuntime(23570): FATAL EXCEPTION: main
07-09 12:52:27.601: E/AndroidRuntime(23570): java.lang.NullPointerException
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.android.internal.view.menu.MenuItemImpl.toString(MenuItemImpl.java:496)
07-09 12:52:27.601: E/AndroidRuntime(23570): at java.lang.StringBuilder.append(StringBuilder.java:202)
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:201)
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
07-09 12:52:27.601: E/AndroidRuntime(23570): at android.view.View.performClick(View.java:4204)
07-09 12:52:27.601: E/AndroidRuntime(23570): at android.view.View$PerformClick.run(View.java:17355)
07-09 12:52:27.601: E/AndroidRuntime(23570): at android.os.Handler.handleCallback(Handler.java:725)
07-09 12:52:27.601: E/AndroidRuntime(23570): at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 12:52:27.601: E/AndroidRuntime(23570): at android.os.Looper.loop(Looper.java:137)
07-09 12:52:27.601: E/AndroidRuntime(23570): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-09 12:52:27.601: E/AndroidRuntime(23570): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 12:52:27.601: E/AndroidRuntime(23570): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-09 12:52:27.601: E/AndroidRuntime(23570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-09 12:52:27.601: E/AndroidRuntime(23570): at dalvik.system.NativeStart.main(Native Method)
编辑,XML 文件:
菜单 XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_refresh"
android:icon="@drawable/ic_action_refresh"
android:orderInCategory="1"
android:showAsAction="always"
/>
</menu>