2016-02-27 88 views
1

我在ToolBar上有購物車圖標。當我嘗試點擊時,它沒有長時間顯示任何操作。繼續點擊它後,我設置的Toast消息被啓用或應用程序崩潰。ToolBar的菜單項不可點擊

請幫我解決它。

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

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

張貼您的回答。它會對某人有用@Edgarprabhu –

+0

不,我沒有得到答案,你找到解決辦法嗎? –

+0

http://stackoverflow.com/questions/37541136/invalidateoptionmenu-disables-the-clicking-functionality-of-toolbar-menu –

回答

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

+0

即使我試過這種方式,但沒有工作。 –

+0

@ParamaSudha你能否粘貼你的整個活動代碼? –

+0

Ya Sure。請等待幾分鐘@Chintan –

0

可能是問題的點擊badge當你不打電話super.onOptionsItemSelected(item)。更改方法如下: -

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

它不能正常工作.. –

+0

你能發佈你的xml嗎? – Frosty

+0

我的xml在這裏..以下鏈接。 @Frosty –

1

問題可能已經被關閉,但我發現可能的原因:

  • 如果您在.xml文件中使用相對佈局,相鄰元素可以覆蓋你的工具欄。 因此,您的工具欄必須位於.xml文件中的視圖之上。 它爲我工作。