我在ToolBar上有購物車圖標。當我嘗試點擊時,它沒有長時間顯示任何操作。繼續點擊它後,我設置的Toast消息被啓用或應用程序崩潰。ToolBar的菜單項不可點擊
請幫我解決它。
我的代碼是:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
mCounter = (TextView) badgeLayout.findViewById(R.id.counter);
badgeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Cart is Working", Toast.LENGTH_SHORT).show();
Intent next = new Intent(context, ProductActivity.class);
startActivity(next);
}
});
return true;
}
UPDATE:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SearchView.OnCloseListener, SearchView.OnQueryTextListener {
public static final String MyPREFERENCES = "Preference";
public static final String SEARCHING_DATA = "DATA_SEARCH";
public static String strTabName;
public static TextView mCounter;
public static RelativeLayout badgeLayout;
public ArrayList<String> values;
Toolbar toolbar;
Context context;
Config config;
ArrayList<String> tabName = new ArrayList<String>();
SharedPreferences pref;
TextView txtDash_Title, txtDash_Des;
String passQuery;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
String possibleEmail;
String GOOGLE_USERNAME;
private int count = 0;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
pref = context.getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
setupToolbar();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
txtDash_Title = (TextView) findViewById(R.id.dashboard_txt_title);
txtDash_Des = (TextView) findViewById(R.id.dashboard_txt_des);
TextView myTextview = (TextView) findViewById(R.id.textView);
if (haveNetworkConnection())
new TabNameSync().execute();
else
Toast.makeText(context, "No Network Connection", Toast.LENGTH_SHORT).show();
setupCollapsingToolbar();
getGOOGLEUSERNAME();
}
private void getGOOGLEUSERNAME() {
Cursor c = getApplication().getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
c.moveToFirst();
GOOGLE_USERNAME = c.getString(c.getColumnIndex("display_name"));
Log.e("Google Username", GOOGLE_USERNAME);
c.close();
}
private void setupCollapsingToolbar() {
final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(
R.id.collapse_toolbar);
collapsingToolbar.setTitleEnabled(false);
}
private void setupToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Welcome to Scoop Shop!");
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
mCounter = (TextView) badgeLayout.findViewById(R.id.counter);
badgeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Cart Working", Toast.LENGTH_SHORT).show();
Intent next = new Intent(context, ProductActivity.class);
startActivity(next);
}
});
// SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
// SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
// searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// searchView.setSubmitButtonEnabled(false);
//// searchView.getOutlineProvider();
// searchView.setOnQueryTextListener(this);
// searchView.setOnCloseListener(this);
// return super.onCreateOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
break;
case R.id.badge:
Toast.makeText(context, "Cart is Working", Toast.LENGTH_SHORT).show();
/* Intent next = new Intent(context, ProductActivity.class);
startActivity(next);*/
return true;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
}
張貼您的回答。它會對某人有用@Edgarprabhu –
不,我沒有得到答案,你找到解決辦法嗎? –
http://stackoverflow.com/questions/37541136/invalidateoptionmenu-disables-the-clicking-functionality-of-toolbar-menu –