无论出于何种原因,当我单击其中一个按钮时,我的 ActionBar 会崩溃。这是 optionsSelectMenu 的代码:
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.refresh_button_actionbar:
if (this.frag instanceof DashboardFragment)
dashboardFrag.refresh();
break;
default:
return false;
}
return true;
}
这是logcat:
07-30 19:17:52.981: E/AndroidRuntime(1358): FATAL EXCEPTION: main
07-30 19:17:52.981: E/AndroidRuntime(1358): java.lang.NullPointerException
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.view.menu.MenuItemImpl.toString(MenuItemImpl.java:496)
07-30 19:17:52.981: E/AndroidRuntime(1358): at java.lang.StringBuilder.append(StringBuilder.java:202)
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:201)
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
07-30 19:17:52.981: E/AndroidRuntime(1358): at android.view.View.performClick(View.java:4204)
07-30 19:17:52.981: E/AndroidRuntime(1358): at android.view.View$PerformClick.run(View.java:17355)
07-30 19:17:52.981: E/AndroidRuntime(1358): at android.os.Handler.handleCallback(Handler.java:725)
07-30 19:17:52.981: E/AndroidRuntime(1358): at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 19:17:52.981: E/AndroidRuntime(1358): at android.os.Looper.loop(Looper.java:137)
07-30 19:17:52.981: E/AndroidRuntime(1358): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 19:17:52.981: E/AndroidRuntime(1358): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 19:17:52.981: E/AndroidRuntime(1358): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 19:17:52.981: E/AndroidRuntime(1358): at dalvik.system.NativeStart.main(Native Method)
关于我做错了什么的任何想法?我认为这与片段比较有关。另外,需要注意的是,当我将代码设置为“return true”时(因为它只会返回 true),它将重新启动活动。这是预期的行为吗?如果是这样,我该如何绕过它?
这是我的进口:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.actifio.android.R;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;