ToolBar的菜单项无法点击

时间:2016-02-27 05:26:11

标签: android android-toolbar cart

我在ToolBar上有购物车图标。当我尝试点击时,它很长时间没有显示任何动作。继续点击它,我设置的Toast消息已启用或应用程序崩溃。

请帮我解决。

enter image description here

我的代码是:

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

更新

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

3 个答案:

答案 0 :(得分:1)

问题可能已经结束,但我找到了可能的原因:

  • 如果您在.xml文件中使用了相对布局,则相邻元素可以覆盖您的工具栏。 因此,您的工具栏必须位于.xml文件中的顶视图中。 它对我有用。

答案 1 :(得分:0)

您应该使用OnOptionsItemSelected()

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        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);
    }
}

有关详细信息,请访问以下链接:http://developer.android.com/guide/topics/ui/menus.html

答案 2 :(得分:0)

问题是您在点击super.onOptionsItemSelected(item)时没有致电badge。按如下方式更改方法: -

@Override
public boolean onOptionsItemSelected(MenuItem item) {
       super.onOptionsItemSelected(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);*/
                   break;
             default:
                   break;
            }
            return true;
     }