所以,我将 jfeinstein10 库用于我的 SlidingMenu。它工作正常,但是当用户点击其中一个菜单选项时,我在切换菜单时遇到问题。
我有滑动菜单所在的 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 指向我调用函数和调用函数内部切换的行。
非常感谢。