对于使用SlidingMenu + ABS的MainActivity类型,未定义getSupportMenuInflater()

时间:2014-10-09 12:52:20

标签: android actionbarsherlock slidingmenu

我正在使用Sliding Menu + ActionBarSherlock,出于某种“奇怪”的原因,我收到了这个错误。我得到“onCreateOptionsMenu”和“onOptionsItemSelected”的类似错误。我认为从ABS导入Menu,MenuInflater和MenuItem会解决问题,但我仍然无法找到解决方案。

我的项目,ABS和SlidingMenu之间的依赖关系不应该有任何问题。此外,我已正确扩展所有类。你们有什么建议吗?谢谢。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity {

    private Fragment mMapFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setBehindContentView(R.layout.menu_frame);

        getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);

        FragmentManager fm = getSupportFragmentManager();
        mMapFragment = fm.findFragmentById(R.id.container);

        if (savedInstanceState == null) {
             mMapFragment = new RunMapFragment();
             fm.beginTransaction()
                .add(R.id.container, mMapFragment)
                .commit();
        }
        getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
        getSlidingMenu().setBehindOffsetRes(R.dimen.slidingmenu_offset);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.button1:
                getSlidingMenu().showMenu();
                return true;
            case R.id.button2:
                getSlidingMenu().showSecondaryMenu();;
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


}

1 个答案:

答案 0 :(得分:0)

SlidingMenuActivity appears to inherit from FragmentActivity,而不是SherlockFragmentActivity,因此您没有ActionBarSherlock支持,例如getSupportMenuInflater()

您可能必须克隆SlidingMenuActivity并创建SherlockSlidingMenuActivity,将其更改为从SherlockFragmentActivity继承,然后使用您的SherlockSlidingMenuActivity作为MainActivity的基类}。