gpt4 book ai didi

android - 关闭滑动菜单 onItemClick

转载 作者:行者123 更新时间:2023-11-29 21:43:33 26 4
gpt4 key购买 nike

因此,我将 jfeinstein10 库用于我的 SlidingMenu。它工作正常,但当用户点击其中一个菜单选项时,我无法切换菜单。

我有滑动菜单所在的 MainActivty 和一个名为 SampleListFragment 的 fragment ,我可以在其中设置菜单选项。

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

我的主要 Activity

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);
}
}

这部分来 self 的 fragment :

@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 指向我调用函数的行以及我在函数内部调用切换的行。

非常感谢。

最佳答案

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

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

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

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

@Wenger 感谢您的帮助。

关于android - 关闭滑动菜单 onItemClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16463677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com