选项菜单(操作栏)未显示

时间:2013-04-16 17:51:50

标签: android android-optionsmenu

我有一个显示Feed项的Activity,它有一个上下文菜单来完成共享,添加到收藏夹等操作。我已将项目从上下文菜单移动到选项菜单,因此可以执行操作直接从操作栏执行。但是选项菜单没有显示在操作栏中(之前确实显示过)。我尚未在项目中添加任何操作,但它们应该显示在选项菜单中。

FeedItemActivity:

 package com.mfavez.android.feedgoal;

 someimports....
 import com.mfavez.android.feedgoal.storage.SharedPreferencesHelper;


 public class FeedItemActivity extends Activity {

private static final String LOG_TAG = "FeedItemActivity";
private static final int KILL_ACTIVITY_CODE = 1;

private DbFeedAdapter mDbFeedAdapter;
private long mItemId = -1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //only show bar for honeycomb and over
    int currentAPIVersion = android.os.Build.VERSION.SDK_INT;

    if (currentAPIVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {

    ActionBar bar = getActionBar();
    bar.setDisplayHomeAsUpEnabled(true);
    bar.show();
    }
    //end of actionbardisplay

    mDbFeedAdapter = new DbFeedAdapter(this);
    mDbFeedAdapter.open();

    TrackerAnalyticsHelper.createTracker(this);

    mItemId = savedInstanceState != null ? savedInstanceState.getLong(DbSchema.ItemSchema._ID) : -1;

    if (mItemId == -1) {
        Bundle extras = getIntent().getExtras();            
        mItemId = extras != null ? extras.getLong(DbSchema.ItemSchema._ID) : -1;
    }

    Item item = mDbFeedAdapter.getItem(mItemId);
    if (item.isFavorite())
        setContentView(R.layout.item_favorite);
    else
        setContentView(R.layout.item_notfavorite);

    /*
     * To test ads in emulator, remove ads:loadAdOnCreate="true" in layout
    if (SharedPreferencesHelper.useAdmob(this)) {
        int resourceId = getResources().getIdentifier("adView", "id", this.getPackageName());
        AdView adView = (AdView)this.findViewById(resourceId);
        //AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest request = new AdRequest();
        request.addTestDevice(AdRequest.TEST_EMULATOR);
        adView.loadAd(request);
    }
    */
    AdRequest request = new AdRequest();
    request.addTestDevice(AdRequest.TEST_EMULATOR);

    TextView title = (TextView)findViewById(R.id.title);
    title.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TrackerAnalyticsHelper.trackEvent(FeedItemActivity.this,LOG_TAG,"Link_Title",mDbFeedAdapter.getItem(mItemId).getLink().toString(),1);
            adjustLinkableTextColor (v);
            startItemWebActivity();
        }
    });

    TextView channel = (TextView)findViewById(R.id.channel);
    channel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Feed feed = mDbFeedAdapter.getFeed(mDbFeedAdapter.getItemFeedId(mItemId));
            String channelHomepage = feed.getHomePage().toString();
            TrackerAnalyticsHelper.trackEvent(FeedItemActivity.this,LOG_TAG,"Link_Channel",channelHomepage,1);
            adjustLinkableTextColor (v);
            if (SharedPreferencesHelper.isOnline(FeedItemActivity.this)) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(channelHomepage));
                startActivity(intent);
            } else
                showDialog(SharedPreferencesHelper.DIALOG_NO_CONNECTION);
        }
    });

    Button read_online = (Button)findViewById(R.id.read);
    read_online.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TrackerAnalyticsHelper.trackEvent(FeedItemActivity.this,LOG_TAG,"Button_ReadOnline",mDbFeedAdapter.getItem(mItemId).getLink().toString(),1);
            startItemWebActivity();
        }
    });

    registerForContextMenu(findViewById(R.id.item));
}

private void startItemWebActivity() {
    if (SharedPreferencesHelper.isOnline(FeedItemActivity.this)) {
        Intent intent = new Intent(FeedItemActivity.this, FeedWebActivity.class);
        intent.putExtra(DbSchema.ItemSchema._ID, mItemId);
        startActivity(intent);
    } else
        showDialog(SharedPreferencesHelper.DIALOG_NO_CONNECTION);
}

private void adjustLinkableTextColor (View v) {
    TextView textView = (TextView) v;
    textView.setTextColor(R.color.color2);

}

private void displayItemView() {
    if (mItemId != -1) {
        Item item = mDbFeedAdapter.getItem(mItemId);
        TextView titleView = (TextView) findViewById(R.id.title);
        TextView channelView = (TextView) findViewById(R.id.channel);
        TextView pubdateView = (TextView) findViewById(R.id.pubdate);
        TextView contentView = (TextView) findViewById(R.id.content);
        if (titleView != null)
            titleView.setText(item.getTitle());
        if (channelView != null) {
            Feed feed = mDbFeedAdapter.getFeed(mDbFeedAdapter.getItemFeedId(mItemId));
            if (feed != null)
                channelView.setText(feed.getTitle());
        } 
        if (pubdateView != null) {
            //DateFormat df = new SimpleDateFormat(getResources().getText(R.string.pubdate_format_pattern);
            //pubdateView.setText(df.format(item.getPubdate()));
            CharSequence formattedPubdate = DateFormat.format(getResources().getText(R.string.pubdate_format_pattern), item.getPubdate());
            pubdateView.setText(formattedPubdate);
        }
        if (contentView != null) {
            String content_description = item.getContent();
            if (content_description == null)
                content_description = item.getDescription();
            if (content_description != null)
                //contentView.setText(content_description,TextView.BufferType.SPANNABLE);
                contentView.setText(content_description);
        }

        // set item as read (case when item is displayed from next/previous contextual menu or buttons)
        ContentValues values = new ContentValues();
        values.put(DbSchema.ItemSchema.COLUMN_READ, DbSchema.ON);
        mDbFeedAdapter.updateItem(mItemId, values, null);

        TrackerAnalyticsHelper.trackPageView(this,"/offlineItemView");
    }
}

@Override
protected void onStart() {
    super.onStart();
    TrackerAnalyticsHelper.startTracker(this);
}

@Override
protected void onResume() {
    super.onResume();
    displayItemView();
}

@Override
protected void onStop() {
    super.onStop();
    TrackerAnalyticsHelper.stopTracker(this);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mDbFeedAdapter.close();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putLong(DbSchema.ItemSchema._ID, mItemId);
}

//---------------------------------------------------------------
//Problems is around here I think!
//---------------------------------------------------------------


public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    Item item = mDbFeedAdapter.getItem(mItemId);

    if (SharedPreferencesHelper.isDynamicMode(this)) {

        if (item != null) {
            long feedId = mDbFeedAdapter.getItemFeedId(mItemId);
            boolean isFirstItem = false;
            boolean isLastItem = false;
            if (mItemId == mDbFeedAdapter.getFirstItem(feedId).getId())
                isFirstItem = true;
            else if (mItemId == mDbFeedAdapter.getLastItem(feedId).getId())
                isLastItem = true;

            if (item.isFavorite()) {
                if (isFirstItem)
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_notfav_next, menu);
                else if (isLastItem)
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_notfav_prev, menu);
                else
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_notfav_next_prev, menu);
            } else {
                if (isFirstItem)
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_fav_next, menu);
                else if (isLastItem)
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_fav_prev, menu);
                else
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_fav_next_prev, menu);
            }
        }

        MenuItem channelsMenuItem = (MenuItem) menu.findItem(R.id.menu_opt_channels);
        channelsMenuItem.setIntent(new Intent(this,FeedChannelsActivity.class));
    } else {

        if (item != null) {
            long feedId = mDbFeedAdapter.getItemFeedId(mItemId);
            boolean isFirstItem = false;
            boolean isLastItem = false;
            if (mItemId == mDbFeedAdapter.getFirstItem(feedId).getId())
                isFirstItem = true;
            else if (mItemId == mDbFeedAdapter.getLastItem(feedId).getId())
                isLastItem = true;

            if (item.isFavorite()) {
                if (isFirstItem)
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_notfav_next, menu);
                else if (isLastItem)
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_notfav_prev, menu);
                else
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_notfav_next_prev, menu);
            } else {
                if (isFirstItem)
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_fav_next, menu);
                else if (isLastItem)
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_fav_prev, menu);
                else
                    inflater.inflate(R.menu.ctx_menu_item_dyn_offline_fav_next_prev, menu);
            }
        }

        // Channels menu item
        if (mDbFeedAdapter.getFeeds().size() > 1) {
            MenuItem channelsMenuItem = (MenuItem) menu.findItem(R.id.menu_opt_channels);
            SubMenu subMenu = channelsMenuItem.getSubMenu();

            List<Feed> feeds = mDbFeedAdapter.getFeeds();
            Iterator<Feed> feedIterator = feeds.iterator();
            Feed feed = null;
            MenuItem channelSubMenuItem = null;
            Intent intent = null;
            int order = 0;
            while (feedIterator.hasNext()) {
                feed = feedIterator.next();
                channelSubMenuItem = subMenu.add(SharedPreferencesHelper.CHANNEL_SUBMENU_GROUP, Menu.NONE, order, feed.getTitle());

                if (feed.getId() == SharedPreferencesHelper.getPrefTabFeedId(this, mDbFeedAdapter.getFirstFeed().getId()))
                    channelSubMenuItem.setChecked(true);

                intent = new Intent(this, FeedTabActivity.class);
                intent.putExtra(DbSchema.FeedSchema._ID, feed.getId());
                channelSubMenuItem.setIntent(intent);

                order++;
            }

            subMenu.setGroupCheckable(SharedPreferencesHelper.CHANNEL_SUBMENU_GROUP, true, true);
        } else {
            menu.removeItem(R.id.menu_opt_channels);
        }
    }

    // Home menu item
    MenuItem menuItem = (MenuItem) menu.findItem(R.id.menu_opt_home);
    menuItem.setIntent(new Intent(this, FeedTabActivity.class));     

    // Preferences menu item
    MenuItem preferencesMenuItem = (MenuItem) menu.findItem(R.id.menu_opt_preferences);
    preferencesMenuItem.setIntent(new Intent(this,FeedPrefActivity.class));

    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
      //using icon to go up in honeycomb and higher
       switch (item.getItemId()) 
       {        
          case android.R.id.home:            
             Intent intent = new Intent(this, FeedTabActivity.class);            
             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
             startActivity(intent);            
             return true;        
          default:            
       }
       //thats it
    switch (item.getItemId()) {
        case R.id.menu_opt_home:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"OptionMenu_Home","Home",1);
            // Kill the FeedTabActivity that started this FeedItemActivity, because tab channel id may have changed and wouldn't be correct (wouldn't be the initial FeedTabActivity channel id) if back button is pressed
            setResult(RESULT_OK);
            startActivity(item.getIntent());
            //finish();
            return true;
        case R.id.menu_opt_channels:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"OptionMenu_Channels","Channels",1);
            if (SharedPreferencesHelper.isDynamicMode(this)) {
                // Kill the FeedTabActivity that started this FeedItemActivity, because tab channel id may have changed and wouldn't be correct (wouldn't be the initial FeedTabActivity channel id) if back button is pressed
                setResult(RESULT_OK);
                startActivityForResult(item.getIntent(),KILL_ACTIVITY_CODE);
            } else {
                //do nothing, default case will be handled
            }
            return true;
        case R.id.menu_opt_preferences:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"OptionMenu_Preferences","Preferences",1);
            startActivity(item.getIntent());
            return true;
        case R.id.menu_opt_about:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"OptionMenu_AboutDialog","About",1);
            showDialog(SharedPreferencesHelper.DIALOG_ABOUT);
            return true;
        default:
            if (item.getGroupId() == SharedPreferencesHelper.CHANNEL_SUBMENU_GROUP) {
                // Kill the FeedTabActivity that started this FeedItemActivity, because tab channel id is now changing and won't be correct (won't be the initial FeedTabActivity channel id) if back button is pressed
                setResult(RESULT_OK);
                startActivity(item.getIntent());
                //finish();
                return true;
            }
    }
    return super.onOptionsItemSelected(item);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch(requestCode) {
        case KILL_ACTIVITY_CODE:
            if (resultCode == RESULT_OK)
                finish();
            break;
        }
}

public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    if (v.getId() == R.id.item) {
        menu.setHeaderTitle (R.string.ctx_menu_title);
        MenuInflater inflater = getMenuInflater();

        Item item = mDbFeedAdapter.getItem(mItemId);

        if (item != null) {
            long feedId = mDbFeedAdapter.getItemFeedId(mItemId);
            boolean isFirstItem = false;
            boolean isLastItem = false;
            if (mItemId == mDbFeedAdapter.getFirstItem(feedId).getId())
                isFirstItem = true;
            else if (mItemId == mDbFeedAdapter.getLastItem(feedId).getId())
                isLastItem = true;

            if (item.isFavorite()) {
                if (isFirstItem)
                    inflater.inflate(R.menu.ctx_menu_item_offline_notfav_next, menu);
                else if (isLastItem)
                    inflater.inflate(R.menu.ctx_menu_item_offline_notfav_prev, menu);
                else
                    inflater.inflate(R.menu.ctx_menu_item_offline_notfav_next_prev, menu);
            } else {
                if (isFirstItem)
                    inflater.inflate(R.menu.ctx_menu_item_offline_fav_next, menu);
                else if (isLastItem)
                    inflater.inflate(R.menu.ctx_menu_item_offline_fav_prev, menu);
                else
                    inflater.inflate(R.menu.ctx_menu_item_offline_fav_next_prev, menu);
            }
        }
    }
}


public boolean onContextItemSelected(MenuItem menuItem) {
    Item item = mDbFeedAdapter.getItem(mItemId);
    ImageView favView = (ImageView) findViewById(R.id.fav);
    ContentValues values = null;
    Intent intent = null;
    long feedId = -1;

    switch (menuItem.getItemId()) {
        case R.id.read_online:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"ContextMenu_ReadOnline",item.getLink().toString(),1);
            startItemWebActivity();
            return true;
        case R.id.add_fav:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"ContextMenu_AddFavorite",item.getLink().toString(),1);
            //item.favorite();
            values = new ContentValues();
            values.put(DbSchema.ItemSchema.COLUMN_FAVORITE, DbSchema.ON);
            mDbFeedAdapter.updateItem(mItemId, values, null);
            favView.setImageResource(R.drawable.fav);
            Toast.makeText(this, R.string.add_fav_msg, Toast.LENGTH_SHORT).show();
            return true;
        case R.id.remove_fav:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"ContextMenu_RemoveFavorite",item.getLink().toString(),1);
            //item.unfavorite();
            Date now = new Date();
            long diffTime = now.getTime() - item.getPubdate().getTime();
            long maxTime = SharedPreferencesHelper.getPrefMaxHours(this) * 60 * 60 * 1000; // Max hours expressed in milliseconds
            // test if item has expired
            if (maxTime > 0 && diffTime > maxTime) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.remove_fav_confirmation)
                       .setCancelable(false)
                       .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                ContentValues values = new ContentValues();
                                values.put(DbSchema.ItemSchema.COLUMN_FAVORITE, DbSchema.OFF);
                                ImageView favView = (ImageView) findViewById(R.id.fav);
                                favView.setImageResource(R.drawable.no_fav);
                                mDbFeedAdapter.updateItem(mItemId, values, null);
                                Toast.makeText(FeedItemActivity.this, R.string.remove_fav_msg, Toast.LENGTH_SHORT).show();
                           }
                       })
                       .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                builder.create().show();
            } else {
                values = new ContentValues();
                values.put(DbSchema.ItemSchema.COLUMN_FAVORITE, DbSchema.OFF);
                mDbFeedAdapter.updateItem(mItemId, values, null);
                favView.setImageResource(R.drawable.no_fav);
                Toast.makeText(this, R.string.remove_fav_msg, Toast.LENGTH_SHORT).show();
            }
            return true;
        case R.id.next:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"ContextMenu_NextItem",item.getLink().toString(),1);
            feedId = mDbFeedAdapter.getItemFeedId(mItemId);
            intent = new Intent(this, FeedItemActivity.class);
            intent.putExtra(DbSchema.ItemSchema._ID, mDbFeedAdapter.getNextItemId(feedId, mItemId));
            startActivity(intent);
            finish();
            return true;
        case R.id.previous:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"ContextMenu_PreviousItem",item.getLink().toString(),1);
            feedId = mDbFeedAdapter.getItemFeedId(mItemId);
            intent = new Intent(this, FeedItemActivity.class);
            intent.putExtra(DbSchema.ItemSchema._ID, mDbFeedAdapter.getPreviousItemId(feedId, mItemId));
            startActivity(intent);
            finish();
            return true;
        case R.id.share:
            TrackerAnalyticsHelper.trackEvent(this,LOG_TAG,"ContextMenu_Share",item.getLink().toString(),1);
            item = mDbFeedAdapter.getItem(mItemId);
            if (item != null) {
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, String.format(getString(R.string.share_subject), getString(R.string.app_name)));
                shareIntent.putExtra(Intent.EXTRA_TEXT, item.getTitle() + " " + item.getLink().toString());
                shareIntent.setType("text/plain");
                startActivity(Intent.createChooser(shareIntent, getString(R.string.share)));
            }
            return true;
        default:
            return super.onContextItemSelected(menuItem);
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    CharSequence title = null;
    LayoutInflater inflater = null;
    View dialogLayout = null;
    AlertDialog.Builder builder = null;
    switch (id) {
        case SharedPreferencesHelper.DIALOG_ABOUT:
            //title = getResources().getText(R.string.app_name) + " - " + getResources().getText(R.string.version) + " " + SharedPreferencesHelper.getVersionName(this);
            title = getString(R.string.app_name) + " - " + getString(R.string.version) + " " + SharedPreferencesHelper.getVersionName(this);

            /*
             * Without cancel button
            dialog = new Dialog(this);
            dialog.setContentView(R.layout.dialog_about);
            dialog.setTitle(title);
            */
            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            dialogLayout = inflater.inflate(R.layout.dialog_about, null);
            TextView childView = null;
            if (getString(R.string.website).equals("")) {
                childView = (TextView) dialogLayout.findViewById(R.id.website);
                childView.setVisibility(View.GONE);
            }
            if (getString(R.string.email).equals("")) {
                childView = (TextView) dialogLayout.findViewById(R.id.email);
                childView.setVisibility(View.GONE);
            }
            if (getString(R.string.contact).equals("")) {
                childView = (TextView) dialogLayout.findViewById(R.id.contact);
                childView.setVisibility(View.GONE);
            }
            if (getString(R.string.powered).equals("")) {
                childView = (TextView) dialogLayout.findViewById(R.id.powered);
                childView.setVisibility(View.GONE);
            }
            builder = new AlertDialog.Builder(this);
            builder.setView(dialogLayout)
                   .setTitle(title)
                   .setIcon(R.drawable.ic_dialog)
                   .setNeutralButton(R.string.close, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            dialog = builder.create();
            break;
        case SharedPreferencesHelper.DIALOG_NO_CONNECTION:
            title = getString(R.string.error);
            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            dialogLayout = inflater.inflate(R.layout.dialog_no_connection, null);
            builder = new AlertDialog.Builder(this);
            builder.setView(dialogLayout)
                   .setTitle(title)
                   .setIcon(R.drawable.ic_dialog)
                   .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            dialog = builder.create();
            break;
        default:
            dialog = null;
        }
      return dialog;
   }
 }

我的旧FeedItemActivity: http://pastebin.com/4rV9uq15

我有人可以帮助我,非常感谢你!

2 个答案:

答案 0 :(得分:0)

如果你想在底部的操作栏中设置你的菜单,请在清单中使用它。

android:uiOptions="splitActionBarWhenNarrow"

或者如果你想在顶部操作栏中设置它,你必须使用xml,否则只有在点击菜单时它才会显示

答案 1 :(得分:0)

我认为问题是你从未夸大你的菜单

public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

MenuInflater inflater = getMenuInflater();
Item item = mDbFeedAdapter.getItem(mItemId);

你创建了你的inflater但是没有使用它。将其更改为

public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

MenuInflater inflater = getMenuInflater();
inflater.inflate((R.menu.mainmenu, menu);    // you forgot this line here where mainmenu is your menu xml
Item item = mDbFeedAdapter.getItem(mItemId);