2013-05-09 161 views
1

因此,我爲我的SlidingMenu使用jfeinstein10庫。它工作正常,但我有一個問題,當用戶點擊其中一個菜單選項時切換菜單。關閉滑動菜單onItemClick

我有滑動菜單的MainActivty和一個名爲SampleListFragment的片段,我設置了菜單選項。

我想要做的是從MainActivity調用一個函數,當我點擊該選項。這個函數只能切換菜單,但是我得到一個NullPointException錯誤。

我的MainActivity

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是全球性的,它的insance我在onCreateView一樣。

NPE指向我稱之爲函數的行,以及我稱之爲函數內部的切換點的行。

非常感謝。

+0

看起來像是因爲你的SlidingMenu對象是在你的活動中定義的,而不是在你的片段中定義的。如果您將片段定義爲與您的活動相同的類別,則可能有效。這或者在片段中使用接口在您單擊列表項時回調活動。 – Wenger 2013-05-09 14:48:41

回答