0

我已经能够在我的操作栏上放置共享小部件以及 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.

这是应用程序上的屏幕元素如何变灰的屏幕截图:

在此处输入图像描述

而且,如果我在代码中添加调试语句,似乎 onNavigationItemSelected() 方法会在每分钟 100 次左右的循环中不断被调用。

谢谢!- 亚历克斯

4

1 回答 1

1

看来您已经注释掉了实际连接导航侦听器的行:

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

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

此外,不确定您是否忘记了and@Override上的注释,但它没有显示。onNavigationItemSelectedonCreateOptionsMenu

于 2013-06-27T11:33:50.203 回答