openOptionsMenu(); onCreate中的错误()

时间:2012-02-07 11:04:01

标签: android optionmenu

我正在尝试在OptionsMenu启动时动态创建Activity。在onCreate我打电话:

openOptionsMenu(); //here i get the error
closeOptionsMenu();

创建菜单,但我收到以下错误:

02-07 12:50:47.183: E/AndroidRuntime(31427): FATAL EXCEPTION: main
02-07 12:50:47.183: E/AndroidRuntime(31427): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.donauinvest.idealsfa/com.donauinvest.idealsfa.forme.ComenziForm}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.os.Looper.loop(Looper.java:150)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.app.ActivityThread.main(ActivityThread.java:4389)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at java.lang.reflect.Method.invoke(Method.java:507)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at dalvik.system.NativeStart.main(Native Method)
02-07 12:50:47.183: E/AndroidRuntime(31427): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.view.ViewRoot.setView(ViewRoot.java:562)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:185)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:97)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.view.Window$LocalWindowManager.addView(Window.java:433)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:563)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:439)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.app.Activity.openOptionsMenu(Activity.java:2486)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at com.donauinvest.idealsfa.forme.ComenziForm.onCreate(ComenziForm.java:130)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
02-07 12:50:47.183: E/AndroidRuntime(31427):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
02-07 12:50:47.183: E/AndroidRuntime(31427):    ... 11 more

我正在创建这样的菜单:

android.view.Menu mymenu=null;
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) 
{
    menu.clear();
    SubMenu opMenu =  menu.addSubMenu(0, 0, 0, "Operatii");
    int lastindex=0;
    if (configurare.bChooseTipCda && istoric==0 && (clientid > 0))
    {
         TipComandaClass.setTipComandaContextForDB(this);
                tc = TipComandaClass.get_TipComanda();

         if (tc.size()>0)
         {
            for (int i = 0; i < tc.size(); i++)
            {
                opMenu.add(0,i,0,tc.get(i).getDenumire());
                lastindex=i;
            }
            lastindex++;
            opMenu.add(0,lastindex,0,"NOU");
            lastindex++;
            opMenu.add(0,lastindex,0,"DETALII");
         }
      }
      if (istoric==1 && configurare.bRetransmitereComanda)
      {    
          lastindex++;
          opMenu.add(0,lastindex,0,"RETRANSMITERE");
      }
      if (istoric == 1 && configurare.bInserareComandaIstoric)
      {
          lastindex++;
          opMenu.add(0,lastindex,0,"INSEREAZA");
      }  
      mymenu=menu;

      if (clientid > 0)
      {
          for (int i=0;i<mymenu.getItem(0).getSubMenu().size();i++)
          {
          if (mymenu.getItem(0).getSubMenu().getItem(i).getTitle().equals("NOU"))
          {
                  mymenu.getItem(0).getSubMenu().getItem(i).setEnabled(true);
              break;
          }
          }
      }
      else
      {
          for (int i=0;i<mymenu.getItem(0).getSubMenu().size();i++)
                     {
                         if (mymenu.getItem(0).getSubMenu().getItem(i).getTitle().equals("NOU"))
                         {
                             mymenu.getItem(0).getSubMenu().getItem(i).setEnabled(false);
                             break;
                         }
                     }
              }

              if (listCom.size() > 0)
              {
                 for (int i=0;i<mymenu.getItem(0).getSubMenu().size();i++)
                 {
                     if (mymenu.getItem(0).getSubMenu().getItem(i).getTitle().equals("DETALII"))
                     {
                         mymenu.getItem(0).getSubMenu().getItem(i).setEnabled(true);
                         break;
                     }
                 }
              }
              else 
              {
                 for (int i=0;i<mymenu.getItem(0).getSubMenu().size();i++)
                 {
                     if (mymenu.getItem(0).getSubMenu().getItem(i).getTitle().equals("DETALII"))
                     {
                         mymenu.getItem(0).getSubMenu().getItem(i).setEnabled(false);
                         break;
                     }
                 }
              }
              if (istoric == 1)
              {
                  for (int i=0;i<mymenu.getItem(0).getSubMenu().size();i++)
                 {
                     if (mymenu.getItem(0).getSubMenu().getItem(i).getTitle().equals("NOU"))
                     {
                         mymenu.getItem(0).getSubMenu().getItem(i).setEnabled(false);
                         break;
                     }
                 }
              }

              if (listCom.size() > 0)
              {
                     mymenu.setGroupVisible(0, true);
              }
              else
              {
                     mymenu.setGroupVisible(0, false);
              }
            super.onCreateOptionsMenu(menu);
            return true;
      }//onCreateOptionsMenu

感谢高级!

1 个答案:

答案 0 :(得分:0)

看起来在openOptionsMenu()中执行onCreate会导致问题。我通过将呼叫转移到onResume

成功地绕过了这个问题
@Override
protected void onResume() {
    super.onResume();
    openOptionsMenu();
}