Actionbarsherlock:菜单按钮在Froyo中工作但不在ICS中工作?

时间:2012-09-14 19:37:00

标签: android button actionbarsherlock

我已经创建了一个适用于Froyo及以下的应用OB Nyt - 但是在ICS及以上版本中,当我点击菜单按钮(更新链接,图像活动链接和搜索链接)时没有任何反应活动)。当我点击ICS中的按钮时,手机上的按钮会亮起 - 但活动不会像在Froyo上那样打开。我很少修改ActionBarSherlock。我从吐司的消息中知道点击是在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上测试了Jelly Bean,并在朋友的三星Galaxy II和III上测试了ICS,结果完全相同。

[编辑]:这使它成功:

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

而不是

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

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

2 个答案:

答案 0 :(得分:1)

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

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

考虑为您的菜单项提供一个ID,并使用它来确定单击的是哪个。

答案 1 :(得分:-2)

这使它起作用:

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

而不是

if (item.toString()=="Save")
相关问题