关闭滑动菜单onItemClick

时间:2013-05-09 14:03:58

标签: android fragment slidingmenu

所以,我正在为我的SlidingMenu使用jfeinstein10库。它工作正常,但当用户点击其中一个菜单选项时,我在切换菜单时遇到问题。

我有使用slidemenu的MainActivty和一个名为SampleListFragment的片段,我在其中设置了菜单选项。

我要做的是在单击选项时从MainActivity调用一个函数。此函数应仅切换菜单,但我得到NullPointException错误。

我的主要活动

public class MainActivity extends BaseActivity implements SlidingActivityBase {

private SlidingMenu menu;

private ImageButton btn_pesquisa;
private ImageButton btn_toggle;

private MakeMateria makeMat = new MakeMateria();

private static final int SCREEN_ORIENTATION_PORTRAIT = 1;

String id_test;

SampleListFragment listFragment = new SampleListFragment();

public MainActivity() {
    super(R.string.title_bar_content);
}

public void mainToggle() {
    Log.d("1", "" + this);
    toggle();
    Log.d("2", "" + this);
}

public static Intent newInstance(Activity activity, int pos) {
    Intent intent = new Intent(activity, MainActivity.class);
    intent.putExtra("pos", pos);
    return intent;
}

public void testeEvent(){
    Log.d("Funciona","works");

    toggle();
}

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



    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(
                R.drawable.titlebar);
        bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
        getSupportActionBar().setBackgroundDrawable(bg);

        BitmapDrawable bgSplit = (BitmapDrawable) getResources()
                .getDrawable(R.drawable.titlebar);
        bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
        getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
    }

    int pos = 0;
    if (getIntent().getExtras() != null) {
        pos = getIntent().getExtras().getInt("pos");
    }

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.abs_layout);

    menu = new SlidingMenu(this);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    menu.setShadowDrawable(R.drawable.shadow);
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    menu.setFadeDegree(0.35f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setBehindScrollScale((float) 1.0);
    menu.setMenu(R.layout.menu_frame);

    // set the Above View
    setContentView(R.layout.content_frame);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new MainFragment()).commit();

    setSlidingActionBarEnabled(true);

    btn_pesquisa = (ImageButton) findViewById(R.id.btnPesquisa);
    btn_toggle = (ImageButton) findViewById(R.id.btn_menu);

    btn_toggle.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            toggle();

        }
    });

    btn_pesquisa.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getApplicationContext(),
                    SearchActivity.class);
            //startActivity(intent);
            overridePendingTransition(R.anim.view_transition_in_left,
                    R.anim.view_transition_out_left);
        }
    });
}

public void getMenu(){
    menu.toggle();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        toggle();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

}

这部分来自我的片段:

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {

    Fragment newContent = null;


    android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();

    switch (position) {
    case 0:
        Log.d("1", "1");        

        getFragmentManager().beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
        mainActivity.getMenu();

        break;
    case 1:
        Log.d("2", "2");
        toggle();
        break;
    case 2:
        Log.d("3", "3");
        toggle();
        break;
    case 3:
        Log.d("4", "4");
        toggle();
        break;
    case 4:
        Log.d("5", "5");
        toggle();
        break;
    case 5:
        Log.d("6", "6");
        toggle();
        break;
    case 6:
        Log.d("7", "7");
        toggle();
        break;
    case 7:
        Log.d("8", "8");
        toggle();
        break;
    case 8:
        Log.d("9", "9");
        toggle();
        break;
    }
    if (newContent != null)
        switchFragment(newContent);
}

MainActivity mainActivity是全局的,我在onCreateView中做了它的内容。

NPE指向我调用函数的行以及函数内部切换的位置。

非常感谢。

1 个答案:

答案 0 :(得分:2)

我这样做了 - &gt; Android : Accessing container activity object from fragment using putExtra?

问题是我传递了一个空对象,但是当我这样做时 - &gt;

((MainActivity) this.getActivity()).getMenu()

我能够从对象中获取正确的值。

@Wenger感谢您的帮助。