我正在以编程方式向操作栏添加项目。我正在使用ViewBadger()标记操作栏中的图标。我在尝试设置MenuItem.setShowAsActionFlags()时得到IndexOutOfBoundsException。如果我注释掉这一行,我就没有错误。在我能看到的代码中没有发生异常,但是在系统代码中。
从logcat,ViewGroup.addInArray()发生错误。可能是什么问题?
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
LinearLayout container = new LinearLayout(this);
container.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
int count = 0;
//Battery///////////////////////////
MenuItem menuItemBattery = menu.add(0,Menu.NONE,count,"Battery Level");
menuItemBattery.setIcon(getResources().getDrawable(R.drawable.ic_action_battery));
count++;
//menuItemBattery.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// The badge view requires target view (iconView in this case)
// to have a ViewGroup parent
ImageButton iconViewBattery = new ImageButton(this);
iconViewBattery.setImageDrawable(menuItemBattery.getIcon());
container.addView(iconViewBattery);
BadgeView badge2 = new BadgeView(this, iconViewBattery);
if (batteryPercent > 30) {
badge2.setTextColor(Color.WHITE);
badge2.setBadgeBackgroundColor(Color.GREEN);
} else if (batteryPercent > 10 && batteryPercent < 31) {
badge2.setTextColor(Color.BLACK);
badge2.setBadgeBackgroundColor(Color.YELLOW);
} else if (batteryPercent < 11) {
badge2.setTextColor(Color.BLACK);
badge2.setBadgeBackgroundColor(Color.RED);
}
badge2.setText(String.valueOf(batteryPercent));
badge2.setTextSize(12);
badge2.show();
menuItemBattery.setActionView(container);
//subject
if (!GlobalVars.subjectGUID.equalsIgnoreCase("")) {
MenuItem menuItemSubject = menu.add(0,Menu.NONE,count,"Subject");
count++;
menuItemSubject.setIcon(getResources().getDrawable(R.drawable.ic_action_person));
menuItemSubject.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
ImageButton iconViewSubject = new ImageButton(this);
iconViewSubject.setImageDrawable(menuItemSubject.getIcon());
container.addView(iconViewSubject);
menuItemSubject.setActionView(container);
}
//new data
if (GlobalVars.newdata.equalsIgnoreCase("1")) {
MenuItem menuItemNewData = menu.add(0,Menu.NONE,count,"New Data");
count++;
menuItemNewData.setIcon(getResources().getDrawable(R.drawable.ic_action_import_export));
menuItemNewData.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
ImageButton iconViewNewData = new ImageButton(this);
iconViewNewData.setImageDrawable(menuItemNewData.getIcon());
container.addView(iconViewNewData);
menuItemNewData.setActionView(container);
}
//Airplane Mode
//////////////////////////////////////////////////////////////
if (noConnectivity && airplaneModeOn) {
MenuItem menuItemAirplane = menu.add(0,Menu.NONE,count,"Airplane Mode");
count++;
menuItemAirplane.setIcon(getResources().getDrawable(R.drawable.ic_action_airplane_mode_on));
//menuItemAirplane.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
ImageButton iconView = new ImageButton(this);
iconView.setImageDrawable(menuItemAirplane.getIcon());
menuItemAirplane.setActionView(container);
}
return true;
}
LogCat:
10-30 09:55:26.277: E/AndroidRuntime(10834): FATAL EXCEPTION: main
10-30 09:55:26.277: E/AndroidRuntime(10834): java.lang.IndexOutOfBoundsException: index=1 count=0
10-30 09:55:26.277: E/AndroidRuntime(10834): at android.view.ViewGroup.addInArray(ViewGroup.java:3426)
10-30 09:55:26.277: E/AndroidRuntime(10834): at android.view.ViewGroup.addViewInner(ViewGroup.java:3361)
10-30 09:55:26.277: E/AndroidRuntime(10834): at android.view.ViewGroup.addView(ViewGroup.java:3210)
10-30 09:55:26.277: E/AndroidRuntime(10834): at android.view.ViewGroup.addView(ViewGroup.java:3155)
10-30 09:55:26.277: E/AndroidRuntime(10834): at com.android.internal.view.menu.BaseMenuPresenter.addItemView(BaseMenuPresenter.java:129)
10-30 09:55:26.277: E/AndroidRuntime(10834): at com.android.internal.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:103)
10-30 09:55:26.277: E/AndroidRuntime(10834): at com.android.internal.view.menu.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:188)
10-30 09:55:26.277: E/AndroidRuntime(10834): at com.android.internal.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:244)
10-30 09:55:26.277: E/AndroidRuntime(10834): at com.android.internal.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:946)
10-30 09:55:26.277: E/AndroidRuntime(10834): at com.android.internal.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:969)
10-30 09:55:26.277: E/AndroidRuntime(10834): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:450)
10-30 09:55:26.277: E/AndroidRuntime(10834): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:769)
10-30 09:55:26.277: E/AndroidRuntime(10834): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:201)
10-30 09:55:26.277: E/AndroidRuntime(10834): at android.os.Handler.handleCallback(Handler.java:725)
10-30 09:55:26.277: E/AndroidRuntime(10834): at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 09:55:26.277: E/AndroidRuntime(10834): at android.os.Looper.loop(Looper.java:137)
编辑:查看问题的源代码,问题似乎就在这里:
/**
* Add an item view at the given index.
*
* @param itemView View to add
* @param childIndex Index within the parent to insert at
*/
protected void addItemView(View itemView, int childIndex) {
final ViewGroup currentParent = (ViewGroup) itemView.getParent();
if (currentParent != null) {
currentParent.removeView(itemView); //INDEX OUT OF BOUNDS
}
((ViewGroup) mMenuView).addView(itemView, childIndex);
}
答案 0 :(得分:0)
似乎我没有所有菜单项的父级,只有其中一些。为每个项目添加布局会阻止IndexOutOfBounds。代码:
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.home, menu);
MenuItem menuItemNewData;
MenuItem menuItemSubject;
MenuItem menuItemBattery;
MenuItem menuItemAirplane;
LinearLayout container = new LinearLayout(this);
container.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
container.setBackgroundColor(getResources().getColor(R.color.headerPalePurple));
int count = 0;
//Battery///////////////////////////
menuItemBattery = menu.add(0,Menu.NONE,count,"Battery Level");
menuItemBattery.setIcon(getResources().getDrawable(R.drawable.ic_action_battery));
count++;
menuItemBattery.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// The badge view requires target view (iconView in this case)
// to have a ViewGroup parent
ImageButton iconViewBattery = new ImageButton(this);
iconViewBattery.setImageDrawable(menuItemBattery.getIcon());
container.addView(iconViewBattery);
BadgeView badge2 = new BadgeView(this, iconViewBattery);
if (batteryPercent > 30) {
badge2.setTextColor(Color.WHITE);
badge2.setBadgeBackgroundColor(Color.GREEN);
} else if (batteryPercent > 10 && batteryPercent < 31) {
badge2.setTextColor(Color.BLACK);
badge2.setBadgeBackgroundColor(Color.YELLOW);
} else if (batteryPercent < 11) {
badge2.setTextColor(Color.BLACK);
badge2.setBadgeBackgroundColor(Color.RED);
}
badge2.setText(String.valueOf(batteryPercent));
badge2.setTextSize(12);
badge2.show();
menuItemBattery.setActionView(container);
//subject
if (!GlobalVars.subjectGUID.equalsIgnoreCase("")) {
LinearLayout container2 = new LinearLayout(this);
container2.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
container2.setBackgroundColor(getResources().getColor(R.color.headerPalePurple));
menuItemSubject = menu.add(0,Menu.NONE,count,"Subject");
menuItemSubject.setIcon(getResources().getDrawable(R.drawable.ic_action_person));
ImageButton iconViewSubject = new ImageButton(this);
iconViewSubject.setImageDrawable(menuItemSubject.getIcon());
container.addView(iconViewSubject);
count++;
menuItemSubject.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
menuItemSubject.setActionView(container2);
}
//new data
if (GlobalVars.newdata.equalsIgnoreCase("1")) {
LinearLayout container3 = new LinearLayout(this);
container3.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
container3.setBackgroundColor(getResources().getColor(R.color.headerPalePurple));
menuItemNewData = menu.add(0,Menu.NONE,count,"New Data");
menuItemNewData.setIcon(getResources().getDrawable(R.drawable.ic_action_import_export));
ImageButton iconViewNewData = new ImageButton(this);
iconViewNewData.setImageDrawable(menuItemNewData.getIcon());
container.addView(iconViewNewData);
//container.addView((View)menuItemNewData);
count++;
menuItemNewData.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
menuItemNewData.setActionView(container3);
}
//Airplane Mode
//////////////////////////////////////////////////////////////
if (noConnectivity && airplaneModeOn) {
LinearLayout container4 = new LinearLayout(this);
container4.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
container4.setBackgroundColor(getResources().getColor(R.color.headerPalePurple));
menuItemAirplane = menu.add(0,Menu.NONE,count,"Airplane Mode");
menuItemAirplane.setIcon(getResources().getDrawable(R.drawable.ic_action_airplane_mode_on));
ImageButton iconViewAirplane = new ImageButton(this);
iconViewAirplane.setImageDrawable(menuItemAirplane.getIcon());
container4.addView(iconViewAirplane);
count++;
menuItemAirplane.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
menuItemAirplane.setActionView(container4);
}
return true;
}