if块中的逻辑错误

时间:2014-12-04 08:01:11

标签: java android

在下面的代码中,当选择了菜单项delete时,代码会执行,但如果是代码的一部分,也会跳转到else。

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
     int menuItemIndex = item.getItemId();
     final AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); 
     //final int pos = menuInfo.position;
     final String menuItemName =menuItems[menuItemIndex];
     if(menuItemName.equals("Delete"))
     {
         try{
         int pos = menuInfo.position;
         Locations l= (Locations)locations.get(pos);
         int id= l.get_id();
        if( db.deleteLocation(id))
            {
            locations.remove(pos);
            s.remove(pos);
            arrayAdapter.notifyDataSetChanged();


            }else


           {

            Toast.makeText(this,"Can not delet a location  with apartment blocks",Toast.LENGTH_LONG).show();

        }

         }catch(Exception e)
         {
             e.printStackTrace();
         }
     }else if((menuItemName.equals("View Receipts")));
     {

         // get an extral to pass to the intent
         int pos1 = menuInfo.position;
         Locations l= (Locations)locations.get(pos1);
         int id= l.get_id();
         Intent intent = new Intent(this,LocationSales.class);
         intent.putExtra("ID", id);
         startActivity(intent);


     }


    return super.onContextItemSelected(item);
}

如果此if( db.deleteLocation(id))表达式为false,则执行Toast部分并在else if块中跳转到此部分

int pos1 = menuInfo.position;
             Locations l= (Locations)locations.get(pos1);
             int id= l.get_id();
             Intent intent = new Intent(this,LocationSales.class);
             intent.putExtra("ID", id);
             startActivity(intent);

我的ifs中有任何缺陷造成这种情况吗?

罗纳德

1 个答案:

答案 0 :(得分:9)

删除分号:

变化

}else if((menuItemName.equals("View Receipts")));

}else if((menuItemName.equals("View Receipts")))

该半冒号结束else if语句,导致后续块始终执行。