【问题标题】:Actionbarsherlock: Menu buttons work in Froyo but not ICS?Actionbarsherlock:菜单按钮在 Froyo 中有效,但在 ICS 中无效?
【发布时间】:2012-09-18 16:00:18
【问题描述】:

我制作了一个应用程序OB Nyt,它在 Froyo 及以下版本上运行良好 - 但在 ICS 及以上版本上,当我单击菜单按钮时没有任何反应(更新链接、图像活动链接和搜索活动链接) .当我单击 ICS 中的按钮时,手机上的按钮会亮起 - 但活动不会像在 Froyo 上那样打开。我对 ActionBarSherlock 的修改很少。我从 toast 消息中知道点击已在 ICS 中注册。但是活动没有开始。在 LogCat 我得到一个

window already focused ignoring focus gain of com.android.internal.view.iinputmethodclient

每次我点击其中一个按钮。我猜测问题可能就在这里:

public boolean onCreateOptionsMenu(Menu menu) {
    //Used to put dark icons on light action bar
    boolean isLight = true;


    menu.add("Save")
        .setIcon(isLight ? R.drawable.ic_stilling1 : R.drawable.ic_stilling1)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    menu.add("Search")
        .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    menu.add("Opdatér")
        .setIcon(isLight ? R.drawable.ic_refresh_inverse : R.drawable.ic_refresh)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    //This uses the imported MenuItem from ActionBarSherlock
    Toast.makeText(this, "Got click: " + item.toString(), Toast.LENGTH_SHORT).show();

    if (item.toString()=="Save"){

        startActivity (new Intent(getApplicationContext(), F3Activity.class));

        return true;
    }

    if (item.toString()=="Search"){

        startActivity (new Intent(getApplicationContext(), SearchActivity.class));

        return true;
    }

    if (item.toString()=="Opdatér"){

        startActivity (new Intent(getApplicationContext(), ABSTabsViewPagerActivity.class));

        return true;
    }



    return true;
}

如您所见,我是编程菜鸟 :-) 有没有人知道如何让按钮在 ICS 中做出反应?我已经在我自己的旧 HTC Desire 和 ICS 上测试了 Jelly Bean,并在我朋友的三星 Galaxy II 和 III 上进行了测试,结果完全相同。

[编辑]:这使它工作:

if (item.getTitle()=="Save")

而不是

if (item.toString()=="Save")

初学者的错误,我知道 ;-)

【问题讨论】:

  • ActionBarSherlock 中 99.9% 的内容可在 2.x 以上的每个平台上运行。菜单不在 .1%
  • 如果我想确保这些活动在 ICS/ABS 中打开,我能否以其他方式开始这些活动?
  • 我也会返回 super.onXYZ 而不是 true 这两种方法 - 特别是如果您的条件之一在第二种方法中不满足。

标签: android button actionbarsherlock


【解决方案1】:

使用String.equals(String) 进行字符串比较。正确的方法是

if ("Save".equals(item.getTitle()))

考虑给你的菜单项一个 ID,并用它来确定哪个被点击了。

【讨论】:

    【解决方案2】:

    这使它起作用了:

    if (item.getTitle()=="Save")
    

    而不是

    if (item.toString()=="Save")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2012-12-03
      • 2019-05-27
      • 1970-01-01
      相关资源
      最近更新 更多