我已经在这几天工作,但没有运气。我正在根据facebook和我的应用程序状态变化的会话实现菜单项的可见性。
我只需要在我的登录活动中隐藏菜单项,然后一旦访问被验证,它当然会显示菜单本身。
我只参加了一项活动:
用于菜单代码:
@SuppressLint({ "InlinedApi", "NewApi" })
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
menu.findItem(R.id.action_previous).setEnabled(viewPager.getCurrentItem() > 0);
// Add either a "next" or "finish" button to the action bar, depending on which page
// is currently selected.
MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE, (viewPager.getCurrentItem() == adapter.getCount() - 1)
? R.string.action_finish
: R.string.action_next);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu1 = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_previous) {
// Go to the previous step in the wizard. If there is no previous step,
// setCurrentItem will do nothing.
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
return true;
} else if (itemId == R.id.action_next) {
// Advance to the next step in the wizard. If there is no next step, setCurrentItem
// will do nothing.
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
return true;
}
return super.onOptionsItemSelected(item);
}
*************************
ViewPager viewPager;
PagerAdapter adapter;
// public int mState = 1;
Menu menu1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupFacebookConnect(savedInstanceState);
int[] bookCover = new int[] {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
};
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(this, bookCover);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@SuppressLint("NewApi")
@Override
public void onPageSelected(int position) {
// When changing pages, reset the action bar actions since they are dependent
// on which page is currently active. An alternative approach is to have each
// fragment expose actions itself (rather than the activity exposing actions),
// but for simplicity, the activity provides the actions in this sample.
//mState = 1;
invalidateOptionsMenu();
}
});
Button facebook_connect = (Button) findViewById(R.id.facebook_login);
facebook_connect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (isFacebookConnected()) {
disConnectFacebook();
} else {
connectFacebook();
}
}
});
}
********************
facebook onState:
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Check if the user is authenticated and
// a deep link needs to be handled.
if (state.isOpened()) {
MenuItem item = menu1.findItem(R.id.action_next);
item.setVisible(true);
viewPager.setVisibility(View.VISIBLE);
} else if (state.isClosed()) {
// Make the recipe list hidden
//
// MenuItem item = menu.findItem(R.menu.main);
// item.setVisible(false);
// viewPager.setVisibility(View.INVISIBLE);
}
}
答案 0 :(得分:1)
在您的代码中,此字段未初始化
MenuItem menu;
所以当你试图调用setVisible()时,onSessionStateChange()中有空指针异常