Android - ActionSherlock导航栏按钮不会调用相应的操作

时间:2013-06-27 11:15:55

标签: android actionbarsherlock android-actionbar

我已经能够在我的操作栏上放置共享小部件和ListNavigation。但是当我点击它们时,分享按钮不会给我分享选项,而ListNavigation不会将我带到我选择的列表项。

以下是我必须设置的代码:

public class MainActivity extends SherlockActivity implements ActionBar.OnNavigationListener
{
    Dialog dialog;

    private TextView mSelected;
    private String[] mLocations;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTheme(R.style.Theme_Sherlock_Light);

        //setContentView(R.layout.list_navigation);
        mSelected = (TextView)findViewById(R.id.text);

        mLocations = getResources().getStringArray(R.array.locations);


        Context context = getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
                context, R.array.locations, R.layout.sherlock_spinner_item);
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getSupportActionBar().setListNavigationCallbacks(list, this);        

and here is my menu inflater and other relevant methods:

    public boolean onCreateOptionsMenu(Menu menu)
    {       
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    private Intent createShareIntent() 
    {   
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain"); //shareIntent.setType("image/*");

        shareIntent.putExtra(Intent.EXTRA_TEXT,"Mobile apps to plan and start your busienss, covering business ideas, business planning, marketing and fundraising.");
        shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Check out http://www.myurl.com");

        return shareIntent;
    }    

当我选择其中一个菜单项时,这个方法永远不会被调用:

public boolean onNavigationItemSelected(int itemPosition, long itemId) 
{        
    if ( itemPosition == 0 )
    {
        Intent myIntent = new Intent(MainActivity.this, MainActivity.class);
        MainActivity.this.startActivity(myIntent);          
    }
    else
    if ( itemPosition == 1 )
    {
        Intent myIntent = new Intent(MainActivity.this, LearnActivity.class);
        MainActivity.this.startActivity(myIntent);          
    }        
    else
    if ( itemPosition == 2 )
    {
        Intent myIntent = new Intent(MainActivity.this, ServicesActivity.class);
        MainActivity.this.startActivity(myIntent);          
    }        
    else
    if ( itemPosition == 3 )
    {
        Intent myIntent = new Intent(MainActivity.this, GiveBackActivity.class);
        MainActivity.this.startActivity(myIntent);
    }                

    return true;
}    

这是我的main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">  

<item android:id="@+id/menu_item_share"         
android:showAsAction="ifRoom"         
android:title="Share"         
android:actionProviderClass="android.widget.ActionProvider" />  

<item
android:id="@+id/menuSort"
android:showAsAction="ifRoom"
android:actionLayout="@layout/action_sort"  />

</menu>

有人知道为什么共享小部件和ListNavigation在选择时都不会调用相应的方法吗?

启动模拟器时,我也会收到这两个错误/警告:

[2013-06-27 08:43:57 - actionbarsherlock] 'default' is not a best match for any device/locale combination.
[2013-06-27 08:43:57 - actionbarsherlock] Displaying it with ', , Locale Language ___Region __, sw320dp, w320dp, h533dp, Normal Screen, Long screen aspect ratio, Portrait Orientation, Normal, Day time, High Density, Finger-based touchscreen, Soft keyboard, No keyboard, Hidden navigation, Trackball navigation, Screen resolution 800x480, API Level 15' which is compatible, but will actually be displayed with another more specific version of the layout.

以下是应用中屏幕元素如何变灰的屏幕截图:

enter image description here

如果我在代码中放置一个调试语句,似乎onNavigationItemSelected()方法会在每分钟左右100次循环中被调用。

谢谢! - 亚历克斯

1 个答案:

答案 0 :(得分:1)

您似乎已注释掉实际连接导航侦听器的行:

// getSupportActionBar().setListNavigationCallbacks(list, this);

我没有看到onOptionsItemSelected(MenuItem),这是按下操作栏按钮时所调用的内容。

此外,不确定您是否忘记@OverrideonNavigationItemSelected上的onCreateOptionsMenu注释,但未显示。